00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027
00028
00029
00030
00031
00032
00033 #include "avcodec.h"
00034 #include "dsputil.h"
00035 #include "internal.h"
00036 #include "imgconvert.h"
00037 #include "libavutil/colorspace.h"
00038 #include "libavutil/pixdesc.h"
00039 #include "libavutil/imgutils.h"
00040
00041 #if HAVE_MMX && HAVE_YASM
00042 #include "x86/dsputil_mmx.h"
00043 #endif
00044
00045 #define FF_COLOR_RGB 0
00046 #define FF_COLOR_GRAY 1
00047 #define FF_COLOR_YUV 2
00048 #define FF_COLOR_YUV_JPEG 3
00050 #if HAVE_MMX && HAVE_YASM
00051 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
00052 #define deinterlace_line ff_deinterlace_line_mmx
00053 #else
00054 #define deinterlace_line_inplace deinterlace_line_inplace_c
00055 #define deinterlace_line deinterlace_line_c
00056 #endif
00057
00058 #define pixdesc_has_alpha(pixdesc) \
00059 ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & PIX_FMT_PAL)
00060
00061 typedef struct PixFmtInfo {
00062 uint8_t color_type;
00063 uint8_t padded_size;
00064 } PixFmtInfo;
00065
00066
00067 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
00068
00069 [PIX_FMT_YUV420P] = {
00070 .color_type = FF_COLOR_YUV,
00071 },
00072 [PIX_FMT_YUV422P] = {
00073 .color_type = FF_COLOR_YUV,
00074 },
00075 [PIX_FMT_YUV444P] = {
00076 .color_type = FF_COLOR_YUV,
00077 },
00078 [PIX_FMT_YUYV422] = {
00079 .color_type = FF_COLOR_YUV,
00080 },
00081 [PIX_FMT_UYVY422] = {
00082 .color_type = FF_COLOR_YUV,
00083 },
00084 [PIX_FMT_YUV410P] = {
00085 .color_type = FF_COLOR_YUV,
00086 },
00087 [PIX_FMT_YUV411P] = {
00088 .color_type = FF_COLOR_YUV,
00089 },
00090 [PIX_FMT_YUV440P] = {
00091 .color_type = FF_COLOR_YUV,
00092 },
00093 [PIX_FMT_YUV420P16LE] = {
00094 .color_type = FF_COLOR_YUV,
00095 },
00096 [PIX_FMT_YUV422P16LE] = {
00097 .color_type = FF_COLOR_YUV,
00098 },
00099 [PIX_FMT_YUV444P16LE] = {
00100 .color_type = FF_COLOR_YUV,
00101 },
00102 [PIX_FMT_YUV420P16BE] = {
00103 .color_type = FF_COLOR_YUV,
00104 },
00105 [PIX_FMT_YUV422P16BE] = {
00106 .color_type = FF_COLOR_YUV,
00107 },
00108 [PIX_FMT_YUV444P16BE] = {
00109 .color_type = FF_COLOR_YUV,
00110 },
00111
00112
00113 [PIX_FMT_YUVA420P] = {
00114 .color_type = FF_COLOR_YUV,
00115 },
00116
00117 [PIX_FMT_YUVA422P] = {
00118 .color_type = FF_COLOR_YUV,
00119 },
00120
00121 [PIX_FMT_YUVA444P] = {
00122 .color_type = FF_COLOR_YUV,
00123 },
00124
00125
00126 [PIX_FMT_YUVJ420P] = {
00127 .color_type = FF_COLOR_YUV_JPEG,
00128 },
00129 [PIX_FMT_YUVJ422P] = {
00130 .color_type = FF_COLOR_YUV_JPEG,
00131 },
00132 [PIX_FMT_YUVJ444P] = {
00133 .color_type = FF_COLOR_YUV_JPEG,
00134 },
00135 [PIX_FMT_YUVJ440P] = {
00136 .color_type = FF_COLOR_YUV_JPEG,
00137 },
00138
00139
00140 [PIX_FMT_RGB24] = {
00141 .color_type = FF_COLOR_RGB,
00142 },
00143 [PIX_FMT_BGR24] = {
00144 .color_type = FF_COLOR_RGB,
00145 },
00146 [PIX_FMT_ARGB] = {
00147 .color_type = FF_COLOR_RGB,
00148 },
00149 [PIX_FMT_RGB48BE] = {
00150 .color_type = FF_COLOR_RGB,
00151 },
00152 [PIX_FMT_RGB48LE] = {
00153 .color_type = FF_COLOR_RGB,
00154 },
00155 [PIX_FMT_RGBA64BE] = {
00156 .color_type = FF_COLOR_RGB,
00157 },
00158 [PIX_FMT_RGBA64LE] = {
00159 .color_type = FF_COLOR_RGB,
00160 },
00161 [PIX_FMT_RGB565BE] = {
00162 .color_type = FF_COLOR_RGB,
00163 },
00164 [PIX_FMT_RGB565LE] = {
00165 .color_type = FF_COLOR_RGB,
00166 },
00167 [PIX_FMT_RGB555BE] = {
00168 .color_type = FF_COLOR_RGB,
00169 .padded_size = 16,
00170 },
00171 [PIX_FMT_RGB555LE] = {
00172 .color_type = FF_COLOR_RGB,
00173 .padded_size = 16,
00174 },
00175 [PIX_FMT_RGB444BE] = {
00176 .color_type = FF_COLOR_RGB,
00177 .padded_size = 16,
00178 },
00179 [PIX_FMT_RGB444LE] = {
00180 .color_type = FF_COLOR_RGB,
00181 .padded_size = 16,
00182 },
00183
00184
00185 [PIX_FMT_GRAY16BE] = {
00186 .color_type = FF_COLOR_GRAY,
00187 },
00188 [PIX_FMT_GRAY16LE] = {
00189 .color_type = FF_COLOR_GRAY,
00190 },
00191 [PIX_FMT_GRAY8] = {
00192 .color_type = FF_COLOR_GRAY,
00193 },
00194 [PIX_FMT_GRAY8A] = {
00195 .color_type = FF_COLOR_GRAY,
00196 },
00197 [PIX_FMT_MONOWHITE] = {
00198 .color_type = FF_COLOR_GRAY,
00199 },
00200 [PIX_FMT_MONOBLACK] = {
00201 .color_type = FF_COLOR_GRAY,
00202 },
00203
00204
00205 [PIX_FMT_PAL8] = {
00206 .color_type = FF_COLOR_RGB,
00207 },
00208 [PIX_FMT_UYYVYY411] = {
00209 .color_type = FF_COLOR_YUV,
00210 },
00211 [PIX_FMT_ABGR] = {
00212 .color_type = FF_COLOR_RGB,
00213 },
00214 [PIX_FMT_BGR48BE] = {
00215 .color_type = FF_COLOR_RGB,
00216 },
00217 [PIX_FMT_BGR48LE] = {
00218 .color_type = FF_COLOR_RGB,
00219 },
00220 [PIX_FMT_BGRA64BE] = {
00221 .color_type = FF_COLOR_RGB,
00222 },
00223 [PIX_FMT_BGRA64LE] = {
00224 .color_type = FF_COLOR_RGB,
00225 },
00226 [PIX_FMT_BGR565BE] = {
00227 .color_type = FF_COLOR_RGB,
00228 .padded_size = 16,
00229 },
00230 [PIX_FMT_BGR565LE] = {
00231 .color_type = FF_COLOR_RGB,
00232 .padded_size = 16,
00233 },
00234 [PIX_FMT_BGR555BE] = {
00235 .color_type = FF_COLOR_RGB,
00236 .padded_size = 16,
00237 },
00238 [PIX_FMT_BGR555LE] = {
00239 .color_type = FF_COLOR_RGB,
00240 .padded_size = 16,
00241 },
00242 [PIX_FMT_BGR444BE] = {
00243 .color_type = FF_COLOR_RGB,
00244 .padded_size = 16,
00245 },
00246 [PIX_FMT_BGR444LE] = {
00247 .color_type = FF_COLOR_RGB,
00248 .padded_size = 16,
00249 },
00250 [PIX_FMT_RGB8] = {
00251 .color_type = FF_COLOR_RGB,
00252 },
00253 [PIX_FMT_RGB4] = {
00254 .color_type = FF_COLOR_RGB,
00255 },
00256 [PIX_FMT_RGB4_BYTE] = {
00257 .color_type = FF_COLOR_RGB,
00258 .padded_size = 8,
00259 },
00260 [PIX_FMT_BGR8] = {
00261 .color_type = FF_COLOR_RGB,
00262 },
00263 [PIX_FMT_BGR4] = {
00264 .color_type = FF_COLOR_RGB,
00265 },
00266 [PIX_FMT_BGR4_BYTE] = {
00267 .color_type = FF_COLOR_RGB,
00268 .padded_size = 8,
00269 },
00270 [PIX_FMT_NV12] = {
00271 .color_type = FF_COLOR_YUV,
00272 },
00273 [PIX_FMT_NV21] = {
00274 .color_type = FF_COLOR_YUV,
00275 },
00276
00277 [PIX_FMT_BGRA] = {
00278 .color_type = FF_COLOR_RGB,
00279 },
00280 [PIX_FMT_RGBA] = {
00281 .color_type = FF_COLOR_RGB,
00282 },
00283 };
00284
00285 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
00286 {
00287 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
00288 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
00289 }
00290
00291 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
00292 {
00293 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
00294 }
00295
00296 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
00297 enum PixelFormat pix_fmt, int width, int height)
00298 {
00299 int ret;
00300
00301 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
00302 return ret;
00303
00304 if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
00305 return ret;
00306
00307 return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
00308 }
00309
00310 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
00311 unsigned char *dest, int dest_size)
00312 {
00313 int i, j, nb_planes = 0, linesizes[4];
00314 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00315 int size = avpicture_get_size(pix_fmt, width, height);
00316
00317 if (size > dest_size || size < 0)
00318 return AVERROR(EINVAL);
00319
00320 for (i = 0; i < desc->nb_components; i++)
00321 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
00322 nb_planes++;
00323
00324 av_image_fill_linesizes(linesizes, pix_fmt, width);
00325 for (i = 0; i < nb_planes; i++) {
00326 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
00327 const unsigned char *s = src->data[i];
00328 h = (height + (1 << shift) - 1) >> shift;
00329
00330 for (j = 0; j < h; j++) {
00331 memcpy(dest, s, linesizes[i]);
00332 dest += linesizes[i];
00333 s += src->linesize[i];
00334 }
00335 }
00336
00337 switch (pix_fmt) {
00338 case PIX_FMT_RGB8:
00339 case PIX_FMT_BGR8:
00340 case PIX_FMT_RGB4_BYTE:
00341 case PIX_FMT_BGR4_BYTE:
00342 case PIX_FMT_GRAY8:
00343
00344 return size;
00345 }
00346
00347 if (desc->flags & PIX_FMT_PAL) {
00348 uint32_t *d32 = (unsigned char *)(((size_t)dest + 3) & ~3);
00349 for (i = 0; i<256; i++)
00350 AV_WL32(d32 + i, AV_RN32(src->data[1] + 4*i));
00351 }
00352
00353 return size;
00354 }
00355
00356 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
00357 {
00358 AVPicture dummy_pict;
00359 if(av_image_check_size(width, height, 0, NULL))
00360 return -1;
00361 if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
00362
00363 return width * height;
00364 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
00365 }
00366
00367 static int get_pix_fmt_depth(int *min, int *max, enum PixelFormat pix_fmt)
00368 {
00369 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00370 int i;
00371
00372 if (!desc->nb_components) {
00373 *min = *max = 0;
00374 return AVERROR(EINVAL);
00375 }
00376
00377 *min = INT_MAX, *max = -INT_MAX;
00378 for (i = 0; i < desc->nb_components; i++) {
00379 *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
00380 *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
00381 }
00382 return 0;
00383 }
00384
00385 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
00386 int has_alpha)
00387 {
00388 const PixFmtInfo *pf, *ps;
00389 const AVPixFmtDescriptor *src_desc;
00390 const AVPixFmtDescriptor *dst_desc;
00391 int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
00392 int ret, loss;
00393
00394 if (dst_pix_fmt >= PIX_FMT_NB || dst_pix_fmt <= PIX_FMT_NONE)
00395 return ~0;
00396
00397 src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
00398 dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
00399 ps = &pix_fmt_info[src_pix_fmt];
00400
00401
00402 loss = 0;
00403
00404 if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
00405 return ret;
00406 if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
00407 return ret;
00408 if (dst_min_depth < src_min_depth ||
00409 dst_max_depth < src_max_depth)
00410 loss |= FF_LOSS_DEPTH;
00411 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
00412 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
00413 loss |= FF_LOSS_RESOLUTION;
00414
00415 pf = &pix_fmt_info[dst_pix_fmt];
00416 switch(pf->color_type) {
00417 case FF_COLOR_RGB:
00418 if (ps->color_type != FF_COLOR_RGB &&
00419 ps->color_type != FF_COLOR_GRAY)
00420 loss |= FF_LOSS_COLORSPACE;
00421 break;
00422 case FF_COLOR_GRAY:
00423 if (ps->color_type != FF_COLOR_GRAY)
00424 loss |= FF_LOSS_COLORSPACE;
00425 break;
00426 case FF_COLOR_YUV:
00427 if (ps->color_type != FF_COLOR_YUV)
00428 loss |= FF_LOSS_COLORSPACE;
00429 break;
00430 case FF_COLOR_YUV_JPEG:
00431 if (ps->color_type != FF_COLOR_YUV_JPEG &&
00432 ps->color_type != FF_COLOR_YUV &&
00433 ps->color_type != FF_COLOR_GRAY)
00434 loss |= FF_LOSS_COLORSPACE;
00435 break;
00436 default:
00437
00438 if (ps->color_type != pf->color_type)
00439 loss |= FF_LOSS_COLORSPACE;
00440 break;
00441 }
00442 if (pf->color_type == FF_COLOR_GRAY &&
00443 ps->color_type != FF_COLOR_GRAY)
00444 loss |= FF_LOSS_CHROMA;
00445 if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
00446 loss |= FF_LOSS_ALPHA;
00447 if (dst_pix_fmt == PIX_FMT_PAL8 &&
00448 (src_pix_fmt != PIX_FMT_PAL8 && (ps->color_type != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
00449 loss |= FF_LOSS_COLORQUANT;
00450
00451 return loss;
00452 }
00453
00454 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
00455 {
00456 const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
00457 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00458
00459 return info->padded_size ?
00460 info->padded_size : av_get_bits_per_pixel(desc);
00461 }
00462
00463 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
00464 int has_alpha, int *loss_ptr)
00465 {
00466 enum PixelFormat dst_pix_fmt;
00467 int i;
00468
00469 if (loss_ptr)
00470 *loss_ptr = 0;
00471
00472 dst_pix_fmt = PIX_FMT_NONE;
00473 for(i = 0; i< FFMIN(PIX_FMT_NB, 64); i++){
00474 if (pix_fmt_mask & (1ULL << i))
00475 dst_pix_fmt = avcodec_find_best_pix_fmt2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
00476 }
00477 return dst_pix_fmt;
00478 }
00479
00480 enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
00481 enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
00482 {
00483 enum PixelFormat dst_pix_fmt;
00484 int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
00485 static const int loss_mask_order[] = {
00486 ~0,
00487 ~FF_LOSS_ALPHA,
00488 ~FF_LOSS_RESOLUTION,
00489 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
00490 ~FF_LOSS_COLORQUANT,
00491 ~FF_LOSS_DEPTH,
00492 ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
00493 ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
00494 FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
00495 0x80000,
00496 0,
00497 };
00498
00499 loss_mask= loss_ptr?~*loss_ptr:~0;
00500 dst_pix_fmt = PIX_FMT_NONE;
00501 loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
00502 loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
00503
00504
00505 for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
00506 loss_order1 = loss1 & loss_mask_order[i];
00507 loss_order2 = loss2 & loss_mask_order[i];
00508
00509 if (loss_order1 == 0 && loss_order2 == 0){
00510 dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
00511 } else if (loss_order1 == 0 || loss_order2 == 0) {
00512 dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
00513 }
00514 }
00515
00516 if (loss_ptr)
00517 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
00518 return dst_pix_fmt;
00519 }
00520
00521 void av_picture_copy(AVPicture *dst, const AVPicture *src,
00522 enum PixelFormat pix_fmt, int width, int height)
00523 {
00524 av_image_copy(dst->data, dst->linesize, src->data,
00525 src->linesize, pix_fmt, width, height);
00526 }
00527
00528
00529 void ff_shrink22(uint8_t *dst, int dst_wrap,
00530 const uint8_t *src, int src_wrap,
00531 int width, int height)
00532 {
00533 int w;
00534 const uint8_t *s1, *s2;
00535 uint8_t *d;
00536
00537 for(;height > 0; height--) {
00538 s1 = src;
00539 s2 = s1 + src_wrap;
00540 d = dst;
00541 for(w = width;w >= 4; w-=4) {
00542 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
00543 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
00544 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
00545 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
00546 s1 += 8;
00547 s2 += 8;
00548 d += 4;
00549 }
00550 for(;w > 0; w--) {
00551 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
00552 s1 += 2;
00553 s2 += 2;
00554 d++;
00555 }
00556 src += 2 * src_wrap;
00557 dst += dst_wrap;
00558 }
00559 }
00560
00561
00562 void ff_shrink44(uint8_t *dst, int dst_wrap,
00563 const uint8_t *src, int src_wrap,
00564 int width, int height)
00565 {
00566 int w;
00567 const uint8_t *s1, *s2, *s3, *s4;
00568 uint8_t *d;
00569
00570 for(;height > 0; height--) {
00571 s1 = src;
00572 s2 = s1 + src_wrap;
00573 s3 = s2 + src_wrap;
00574 s4 = s3 + src_wrap;
00575 d = dst;
00576 for(w = width;w > 0; w--) {
00577 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
00578 s2[0] + s2[1] + s2[2] + s2[3] +
00579 s3[0] + s3[1] + s3[2] + s3[3] +
00580 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
00581 s1 += 4;
00582 s2 += 4;
00583 s3 += 4;
00584 s4 += 4;
00585 d++;
00586 }
00587 src += 4 * src_wrap;
00588 dst += dst_wrap;
00589 }
00590 }
00591
00592
00593 void ff_shrink88(uint8_t *dst, int dst_wrap,
00594 const uint8_t *src, int src_wrap,
00595 int width, int height)
00596 {
00597 int w, i;
00598
00599 for(;height > 0; height--) {
00600 for(w = width;w > 0; w--) {
00601 int tmp=0;
00602 for(i=0; i<8; i++){
00603 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
00604 src += src_wrap;
00605 }
00606 *(dst++) = (tmp + 32)>>6;
00607 src += 8 - 8*src_wrap;
00608 }
00609 src += 8*src_wrap - 8*width;
00610 dst += dst_wrap - width;
00611 }
00612 }
00613
00614
00615 int avpicture_alloc(AVPicture *picture,
00616 enum PixelFormat pix_fmt, int width, int height)
00617 {
00618 int ret;
00619
00620 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
00621 memset(picture, 0, sizeof(AVPicture));
00622 return ret;
00623 }
00624
00625 return 0;
00626 }
00627
00628 void avpicture_free(AVPicture *picture)
00629 {
00630 av_free(picture->data[0]);
00631 }
00632
00633
00634 static inline int is_yuv_planar(enum PixelFormat fmt)
00635 {
00636 const PixFmtInfo *info = &pix_fmt_info[fmt];
00637 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
00638 int i;
00639 int planes[4] = { 0 };
00640
00641 if (info->color_type != FF_COLOR_YUV &&
00642 info->color_type != FF_COLOR_YUV_JPEG)
00643 return 0;
00644
00645
00646 for (i = 0; i < desc->nb_components; i++)
00647 planes[desc->comp[i].plane] = 1;
00648
00649
00650 for (i = 0; i < desc->nb_components; i++)
00651 if (!planes[i])
00652 return 0;
00653 return 1;
00654 }
00655
00656 int av_picture_crop(AVPicture *dst, const AVPicture *src,
00657 enum PixelFormat pix_fmt, int top_band, int left_band)
00658 {
00659 int y_shift;
00660 int x_shift;
00661
00662 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
00663 return -1;
00664
00665 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
00666 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
00667
00668 if (is_yuv_planar(pix_fmt)) {
00669 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
00670 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
00671 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
00672 } else{
00673 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
00674 return -1;
00675 if(left_band)
00676 return -1;
00677 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
00678 }
00679
00680 dst->linesize[0] = src->linesize[0];
00681 dst->linesize[1] = src->linesize[1];
00682 dst->linesize[2] = src->linesize[2];
00683 return 0;
00684 }
00685
00686 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
00687 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
00688 int *color)
00689 {
00690 uint8_t *optr;
00691 int y_shift;
00692 int x_shift;
00693 int yheight;
00694 int i, y;
00695
00696 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
00697 !is_yuv_planar(pix_fmt)) return -1;
00698
00699 for (i = 0; i < 3; i++) {
00700 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
00701 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
00702
00703 if (padtop || padleft) {
00704 memset(dst->data[i], color[i],
00705 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
00706 }
00707
00708 if (padleft || padright) {
00709 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
00710 (dst->linesize[i] - (padright >> x_shift));
00711 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
00712 for (y = 0; y < yheight; y++) {
00713 memset(optr, color[i], (padleft + padright) >> x_shift);
00714 optr += dst->linesize[i];
00715 }
00716 }
00717
00718 if (src) {
00719 uint8_t *iptr = src->data[i];
00720 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
00721 (padleft >> x_shift);
00722 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
00723 iptr += src->linesize[i];
00724 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
00725 (dst->linesize[i] - (padright >> x_shift));
00726 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
00727 for (y = 0; y < yheight; y++) {
00728 memset(optr, color[i], (padleft + padright) >> x_shift);
00729 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
00730 (width - padleft - padright) >> x_shift);
00731 iptr += src->linesize[i];
00732 optr += dst->linesize[i];
00733 }
00734 }
00735
00736 if (padbottom || padright) {
00737 optr = dst->data[i] + dst->linesize[i] *
00738 ((height - padbottom) >> y_shift) - (padright >> x_shift);
00739 memset(optr, color[i],dst->linesize[i] *
00740 (padbottom >> y_shift) + (padright >> x_shift));
00741 }
00742 }
00743 return 0;
00744 }
00745
00746 #if !(HAVE_MMX && HAVE_YASM)
00747
00748 static void deinterlace_line_c(uint8_t *dst,
00749 const uint8_t *lum_m4, const uint8_t *lum_m3,
00750 const uint8_t *lum_m2, const uint8_t *lum_m1,
00751 const uint8_t *lum,
00752 int size)
00753 {
00754 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00755 int sum;
00756
00757 for(;size > 0;size--) {
00758 sum = -lum_m4[0];
00759 sum += lum_m3[0] << 2;
00760 sum += lum_m2[0] << 1;
00761 sum += lum_m1[0] << 2;
00762 sum += -lum[0];
00763 dst[0] = cm[(sum + 4) >> 3];
00764 lum_m4++;
00765 lum_m3++;
00766 lum_m2++;
00767 lum_m1++;
00768 lum++;
00769 dst++;
00770 }
00771 }
00772
00773 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
00774 uint8_t *lum_m2, uint8_t *lum_m1,
00775 uint8_t *lum, int size)
00776 {
00777 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00778 int sum;
00779
00780 for(;size > 0;size--) {
00781 sum = -lum_m4[0];
00782 sum += lum_m3[0] << 2;
00783 sum += lum_m2[0] << 1;
00784 lum_m4[0]=lum_m2[0];
00785 sum += lum_m1[0] << 2;
00786 sum += -lum[0];
00787 lum_m2[0] = cm[(sum + 4) >> 3];
00788 lum_m4++;
00789 lum_m3++;
00790 lum_m2++;
00791 lum_m1++;
00792 lum++;
00793 }
00794 }
00795 #endif
00796
00797
00798
00799
00800 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
00801 const uint8_t *src1, int src_wrap,
00802 int width, int height)
00803 {
00804 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
00805 int y;
00806
00807 src_m2 = src1;
00808 src_m1 = src1;
00809 src_0=&src_m1[src_wrap];
00810 src_p1=&src_0[src_wrap];
00811 src_p2=&src_p1[src_wrap];
00812 for(y=0;y<(height-2);y+=2) {
00813 memcpy(dst,src_m1,width);
00814 dst += dst_wrap;
00815 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
00816 src_m2 = src_0;
00817 src_m1 = src_p1;
00818 src_0 = src_p2;
00819 src_p1 += 2*src_wrap;
00820 src_p2 += 2*src_wrap;
00821 dst += dst_wrap;
00822 }
00823 memcpy(dst,src_m1,width);
00824 dst += dst_wrap;
00825
00826 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
00827 }
00828
00829 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
00830 int width, int height)
00831 {
00832 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
00833 int y;
00834 uint8_t *buf;
00835 buf = av_malloc(width);
00836
00837 src_m1 = src1;
00838 memcpy(buf,src_m1,width);
00839 src_0=&src_m1[src_wrap];
00840 src_p1=&src_0[src_wrap];
00841 src_p2=&src_p1[src_wrap];
00842 for(y=0;y<(height-2);y+=2) {
00843 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
00844 src_m1 = src_p1;
00845 src_0 = src_p2;
00846 src_p1 += 2*src_wrap;
00847 src_p2 += 2*src_wrap;
00848 }
00849
00850 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
00851 av_free(buf);
00852 }
00853
00854 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
00855 enum PixelFormat pix_fmt, int width, int height)
00856 {
00857 int i;
00858
00859 if (pix_fmt != PIX_FMT_YUV420P &&
00860 pix_fmt != PIX_FMT_YUVJ420P &&
00861 pix_fmt != PIX_FMT_YUV422P &&
00862 pix_fmt != PIX_FMT_YUVJ422P &&
00863 pix_fmt != PIX_FMT_YUV444P &&
00864 pix_fmt != PIX_FMT_YUV411P &&
00865 pix_fmt != PIX_FMT_GRAY8)
00866 return -1;
00867 if ((width & 3) != 0 || (height & 3) != 0)
00868 return -1;
00869
00870 for(i=0;i<3;i++) {
00871 if (i == 1) {
00872 switch(pix_fmt) {
00873 case PIX_FMT_YUVJ420P:
00874 case PIX_FMT_YUV420P:
00875 width >>= 1;
00876 height >>= 1;
00877 break;
00878 case PIX_FMT_YUV422P:
00879 case PIX_FMT_YUVJ422P:
00880 width >>= 1;
00881 break;
00882 case PIX_FMT_YUV411P:
00883 width >>= 2;
00884 break;
00885 default:
00886 break;
00887 }
00888 if (pix_fmt == PIX_FMT_GRAY8) {
00889 break;
00890 }
00891 }
00892 if (src == dst) {
00893 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
00894 width, height);
00895 } else {
00896 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
00897 src->data[i], src->linesize[i],
00898 width, height);
00899 }
00900 }
00901 emms_c();
00902 return 0;
00903 }