1/* 2 * utils for libavcodec 3 * Copyright (c) 2001 Fabrice Bellard 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> 5 * 6 * This file is part of FFmpeg. 7 * 8 * FFmpeg is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * FFmpeg is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with FFmpeg; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 */ 22 23/** 24 * @file 25 * utils. 26 */ 27 28/* needed for mkstemp() */ 29#define _XOPEN_SOURCE 600 30 31#include "libavutil/avstring.h" 32#include "libavutil/integer.h" 33#include "libavutil/crc.h" 34#include "libavutil/pixdesc.h" 35#include "avcodec.h" 36#include "dsputil.h" 37#include "opt.h" 38#include "imgconvert.h" 39#include "audioconvert.h" 40#include "libxvid_internal.h" 41#include "internal.h" 42#include <stdlib.h> 43#include <stdarg.h> 44#include <limits.h> 45#include <float.h> 46#if !HAVE_MKSTEMP 47#include <fcntl.h> 48#endif 49 50static int volatile entangled_thread_counter=0; 51int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op); 52static void *codec_mutex; 53 54void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size) 55{ 56 if(min_size < *size) 57 return ptr; 58 59 *size= FFMAX(17*min_size/16 + 32, min_size); 60 61 ptr= av_realloc(ptr, *size); 62 if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now 63 *size= 0; 64 65 return ptr; 66} 67 68void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size) 69{ 70 void **p = ptr; 71 if (min_size < *size) 72 return; 73 *size= FFMAX(17*min_size/16 + 32, min_size); 74 av_free(*p); 75 *p = av_malloc(*size); 76 if (!*p) *size = 0; 77} 78 79/* encoder management */ 80static AVCodec *first_avcodec = NULL; 81 82AVCodec *av_codec_next(AVCodec *c){ 83 if(c) return c->next; 84 else return first_avcodec; 85} 86 87void avcodec_register(AVCodec *codec) 88{ 89 AVCodec **p; 90 avcodec_init(); 91 p = &first_avcodec; 92 while (*p != NULL) p = &(*p)->next; 93 *p = codec; 94 codec->next = NULL; 95} 96 97#if LIBAVCODEC_VERSION_MAJOR < 53 98void register_avcodec(AVCodec *codec) 99{ 100 avcodec_register(codec); 101} 102#endif 103 104unsigned avcodec_get_edge_width(void) 105{ 106 return EDGE_WIDTH; 107} 108 109void avcodec_set_dimensions(AVCodecContext *s, int width, int height){ 110 s->coded_width = width; 111 s->coded_height= height; 112 s->width = -((-width )>>s->lowres); 113 s->height= -((-height)>>s->lowres); 114} 115 116typedef struct InternalBuffer{ 117 int last_pic_num; 118 uint8_t *base[4]; 119 uint8_t *data[4]; 120 int linesize[4]; 121 int width, height; 122 enum PixelFormat pix_fmt; 123}InternalBuffer; 124 125#define INTERNAL_BUFFER_SIZE 32 126 127void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){ 128 int w_align= 1; 129 int h_align= 1; 130 131 switch(s->pix_fmt){ 132 case PIX_FMT_YUV420P: 133 case PIX_FMT_YUYV422: 134 case PIX_FMT_UYVY422: 135 case PIX_FMT_YUV422P: 136 case PIX_FMT_YUV440P: 137 case PIX_FMT_YUV444P: 138 case PIX_FMT_GRAY8: 139 case PIX_FMT_GRAY16BE: 140 case PIX_FMT_GRAY16LE: 141 case PIX_FMT_YUVJ420P: 142 case PIX_FMT_YUVJ422P: 143 case PIX_FMT_YUVJ440P: 144 case PIX_FMT_YUVJ444P: 145 case PIX_FMT_YUVA420P: 146 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment 147 h_align= 16; 148 if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP) 149 h_align= 32; // interlaced is rounded up to 2 MBs 150 break; 151 case PIX_FMT_YUV411P: 152 case PIX_FMT_UYYVYY411: 153 w_align=32; 154 h_align=8; 155 break; 156 case PIX_FMT_YUV410P: 157 if(s->codec_id == CODEC_ID_SVQ1){ 158 w_align=64; 159 h_align=64; 160 } 161 case PIX_FMT_RGB555: 162 if(s->codec_id == CODEC_ID_RPZA){ 163 w_align=4; 164 h_align=4; 165 } 166 case PIX_FMT_PAL8: 167 case PIX_FMT_BGR8: 168 case PIX_FMT_RGB8: 169 if(s->codec_id == CODEC_ID_SMC){ 170 w_align=4; 171 h_align=4; 172 } 173 break; 174 case PIX_FMT_BGR24: 175 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){ 176 w_align=4; 177 h_align=4; 178 } 179 break; 180 default: 181 w_align= 1; 182 h_align= 1; 183 break; 184 } 185 186 *width = FFALIGN(*width , w_align); 187 *height= FFALIGN(*height, h_align); 188 if(s->codec_id == CODEC_ID_H264) 189 *height+=2; // some of the optimized chroma MC reads one line too much 190 191 linesize_align[0] = 192 linesize_align[1] = 193 linesize_align[2] = 194 linesize_align[3] = STRIDE_ALIGN; 195//STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes 196//we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the 197//picture size unneccessarily in some cases. The solution here is not 198//pretty and better ideas are welcome! 199#if HAVE_MMX 200 if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 || 201 s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F || 202 s->codec_id == CODEC_ID_VP6A) { 203 linesize_align[0] = 204 linesize_align[1] = 205 linesize_align[2] = 16; 206 } 207#endif 208} 209 210void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){ 211 int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w; 212 int linesize_align[4]; 213 int align; 214 avcodec_align_dimensions2(s, width, height, linesize_align); 215 align = FFMAX(linesize_align[0], linesize_align[3]); 216 linesize_align[1] <<= chroma_shift; 217 linesize_align[2] <<= chroma_shift; 218 align = FFMAX3(align, linesize_align[1], linesize_align[2]); 219 *width=FFALIGN(*width, align); 220} 221 222int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){ 223 if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8) 224 return 0; 225 226 av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h); 227 return AVERROR(EINVAL); 228} 229 230int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){ 231 int i; 232 int w= s->width; 233 int h= s->height; 234 InternalBuffer *buf; 235 int *picture_number; 236 237 if(pic->data[0]!=NULL) { 238 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n"); 239 return -1; 240 } 241 if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) { 242 av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n"); 243 return -1; 244 } 245 246 if(avcodec_check_dimensions(s,w,h)) 247 return -1; 248 249 if(s->internal_buffer==NULL){ 250 s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer)); 251 } 252#if 0 253 s->internal_buffer= av_fast_realloc( 254 s->internal_buffer, 255 &s->internal_buffer_size, 256 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/ 257 ); 258#endif 259 260 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count]; 261 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack 262 (*picture_number)++; 263 264 if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){ 265 for(i=0; i<4; i++){ 266 av_freep(&buf->base[i]); 267 buf->data[i]= NULL; 268 } 269 } 270 271 if(buf->base[0]){ 272 pic->age= *picture_number - buf->last_pic_num; 273 buf->last_pic_num= *picture_number; 274 }else{ 275 int h_chroma_shift, v_chroma_shift; 276 int size[4] = {0}; 277 int tmpsize; 278 int unaligned; 279 AVPicture picture; 280 int stride_align[4]; 281 282 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift); 283 284 avcodec_align_dimensions2(s, &w, &h, stride_align); 285 286 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){ 287 w+= EDGE_WIDTH*2; 288 h+= EDGE_WIDTH*2; 289 } 290 291 do { 292 // NOTE: do not align linesizes individually, this breaks e.g. assumptions 293 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2 294 ff_fill_linesize(&picture, s->pix_fmt, w); 295 // increase alignment of w for next try (rhs gives the lowest bit set in w) 296 w += w & ~(w-1); 297 298 unaligned = 0; 299 for (i=0; i<4; i++){ 300 unaligned |= picture.linesize[i] % stride_align[i]; 301 } 302 } while (unaligned); 303 304 tmpsize = ff_fill_pointer(&picture, NULL, s->pix_fmt, h); 305 if (tmpsize < 0) 306 return -1; 307 308 for (i=0; i<3 && picture.data[i+1]; i++) 309 size[i] = picture.data[i+1] - picture.data[i]; 310 size[i] = tmpsize - (picture.data[i] - picture.data[0]); 311 312 buf->last_pic_num= -256*256*256*64; 313 memset(buf->base, 0, sizeof(buf->base)); 314 memset(buf->data, 0, sizeof(buf->data)); 315 316 for(i=0; i<4 && size[i]; i++){ 317 const int h_shift= i==0 ? 0 : h_chroma_shift; 318 const int v_shift= i==0 ? 0 : v_chroma_shift; 319 320 buf->linesize[i]= picture.linesize[i]; 321 322 buf->base[i]= av_malloc(size[i]+16); //FIXME 16 323 if(buf->base[i]==NULL) return -1; 324 memset(buf->base[i], 128, size[i]); 325 326 // no edge if EDEG EMU or not planar YUV 327 if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2]) 328 buf->data[i] = buf->base[i]; 329 else 330 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]); 331 } 332 if(size[1] && !size[2]) 333 ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt); 334 buf->width = s->width; 335 buf->height = s->height; 336 buf->pix_fmt= s->pix_fmt; 337 pic->age= 256*256*256*64; 338 } 339 pic->type= FF_BUFFER_TYPE_INTERNAL; 340 341 for(i=0; i<4; i++){ 342 pic->base[i]= buf->base[i]; 343 pic->data[i]= buf->data[i]; 344 pic->linesize[i]= buf->linesize[i]; 345 } 346 s->internal_buffer_count++; 347 348 pic->reordered_opaque= s->reordered_opaque; 349 350 if(s->debug&FF_DEBUG_BUFFERS) 351 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count); 352 353 return 0; 354} 355 356void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){ 357 int i; 358 InternalBuffer *buf, *last; 359 360 assert(pic->type==FF_BUFFER_TYPE_INTERNAL); 361 assert(s->internal_buffer_count); 362 363 buf = NULL; /* avoids warning */ 364 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize 365 buf= &((InternalBuffer*)s->internal_buffer)[i]; 366 if(buf->data[0] == pic->data[0]) 367 break; 368 } 369 assert(i < s->internal_buffer_count); 370 s->internal_buffer_count--; 371 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count]; 372 373 FFSWAP(InternalBuffer, *buf, *last); 374 375 for(i=0; i<4; i++){ 376 pic->data[i]=NULL; 377// pic->base[i]=NULL; 378 } 379//printf("R%X\n", pic->opaque); 380 381 if(s->debug&FF_DEBUG_BUFFERS) 382 av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count); 383} 384 385int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){ 386 AVFrame temp_pic; 387 int i; 388 389 /* If no picture return a new buffer */ 390 if(pic->data[0] == NULL) { 391 /* We will copy from buffer, so must be readable */ 392 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE; 393 return s->get_buffer(s, pic); 394 } 395 396 /* If internal buffer type return the same buffer */ 397 if(pic->type == FF_BUFFER_TYPE_INTERNAL) { 398 pic->reordered_opaque= s->reordered_opaque; 399 return 0; 400 } 401 402 /* 403 * Not internal type and reget_buffer not overridden, emulate cr buffer 404 */ 405 temp_pic = *pic; 406 for(i = 0; i < 4; i++) 407 pic->data[i] = pic->base[i] = NULL; 408 pic->opaque = NULL; 409 /* Allocate new frame */ 410 if (s->get_buffer(s, pic)) 411 return -1; 412 /* Copy image data from old buffer to new buffer */ 413 av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width, 414 s->height); 415 s->release_buffer(s, &temp_pic); // Release old frame 416 return 0; 417} 418 419int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){ 420 int i; 421 422 for(i=0; i<count; i++){ 423 int r= func(c, (char*)arg + i*size); 424 if(ret) ret[i]= r; 425 } 426 return 0; 427} 428 429int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){ 430 int i; 431 432 for(i=0; i<count; i++){ 433 int r= func(c, arg, i, 0); 434 if(ret) ret[i]= r; 435 } 436 return 0; 437} 438 439enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){ 440 while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt)) 441 ++fmt; 442 return fmt[0]; 443} 444 445void avcodec_get_frame_defaults(AVFrame *pic){ 446 memset(pic, 0, sizeof(AVFrame)); 447 448 pic->pts= AV_NOPTS_VALUE; 449 pic->key_frame= 1; 450} 451 452AVFrame *avcodec_alloc_frame(void){ 453 AVFrame *pic= av_malloc(sizeof(AVFrame)); 454 455 if(pic==NULL) return NULL; 456 457 avcodec_get_frame_defaults(pic); 458 459 return pic; 460} 461 462int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec) 463{ 464 int ret= -1; 465 466 /* If there is a user-supplied mutex locking routine, call it. */ 467 if (ff_lockmgr_cb) { 468 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) 469 return -1; 470 } 471 472 entangled_thread_counter++; 473 if(entangled_thread_counter != 1){ 474 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n"); 475 goto end; 476 } 477 478 if(avctx->codec || !codec) 479 goto end; 480 481 if (codec->priv_data_size > 0) { 482 avctx->priv_data = av_mallocz(codec->priv_data_size); 483 if (!avctx->priv_data) { 484 ret = AVERROR(ENOMEM); 485 goto end; 486 } 487 } else { 488 avctx->priv_data = NULL; 489 } 490 491 if(avctx->coded_width && avctx->coded_height) 492 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height); 493 else if(avctx->width && avctx->height) 494 avcodec_set_dimensions(avctx, avctx->width, avctx->height); 495 496#define SANE_NB_CHANNELS 128U 497 if (((avctx->coded_width || avctx->coded_height) 498 && avcodec_check_dimensions(avctx, avctx->coded_width, avctx->coded_height)) 499 || avctx->channels > SANE_NB_CHANNELS) { 500 ret = AVERROR(EINVAL); 501 goto free_and_end; 502 } 503 504 avctx->codec = codec; 505 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) && 506 avctx->codec_id == CODEC_ID_NONE) { 507 avctx->codec_type = codec->type; 508 avctx->codec_id = codec->id; 509 } 510 if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){ 511 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n"); 512 goto free_and_end; 513 } 514 avctx->frame_number = 0; 515 if(avctx->codec->init){ 516 ret = avctx->codec->init(avctx); 517 if (ret < 0) { 518 goto free_and_end; 519 } 520 } 521 ret=0; 522end: 523 entangled_thread_counter--; 524 525 /* Release any user-supplied mutex. */ 526 if (ff_lockmgr_cb) { 527 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); 528 } 529 return ret; 530free_and_end: 531 av_freep(&avctx->priv_data); 532 avctx->codec= NULL; 533 goto end; 534} 535 536int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size, 537 const short *samples) 538{ 539 if(buf_size < FF_MIN_BUFFER_SIZE && 0){ 540 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n"); 541 return -1; 542 } 543 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){ 544 int ret = avctx->codec->encode(avctx, buf, buf_size, samples); 545 avctx->frame_number++; 546 return ret; 547 }else 548 return 0; 549} 550 551int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, 552 const AVFrame *pict) 553{ 554 if(buf_size < FF_MIN_BUFFER_SIZE){ 555 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n"); 556 return -1; 557 } 558 if(avcodec_check_dimensions(avctx,avctx->width,avctx->height)) 559 return -1; 560 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){ 561 int ret = avctx->codec->encode(avctx, buf, buf_size, pict); 562 avctx->frame_number++; 563 emms_c(); //needed to avoid an emms_c() call before every return; 564 565 return ret; 566 }else 567 return 0; 568} 569 570int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, 571 const AVSubtitle *sub) 572{ 573 int ret; 574 if(sub->start_display_time) { 575 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n"); 576 return -1; 577 } 578 if(sub->num_rects == 0 || !sub->rects) 579 return -1; 580 ret = avctx->codec->encode(avctx, buf, buf_size, sub); 581 avctx->frame_number++; 582 return ret; 583} 584 585#if LIBAVCODEC_VERSION_MAJOR < 53 586int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture, 587 int *got_picture_ptr, 588 const uint8_t *buf, int buf_size) 589{ 590 AVPacket avpkt; 591 av_init_packet(&avpkt); 592 avpkt.data = buf; 593 avpkt.size = buf_size; 594 // HACK for CorePNG to decode as normal PNG by default 595 avpkt.flags = AV_PKT_FLAG_KEY; 596 597 return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt); 598} 599#endif 600 601int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, 602 int *got_picture_ptr, 603 AVPacket *avpkt) 604{ 605 int ret; 606 607 *got_picture_ptr= 0; 608 if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)) 609 return -1; 610 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){ 611 ret = avctx->codec->decode(avctx, picture, got_picture_ptr, 612 avpkt); 613 614 emms_c(); //needed to avoid an emms_c() call before every return; 615 616 if (*got_picture_ptr) 617 avctx->frame_number++; 618 }else 619 ret= 0; 620 621 return ret; 622} 623 624#if LIBAVCODEC_VERSION_MAJOR < 53 625int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples, 626 int *frame_size_ptr, 627 const uint8_t *buf, int buf_size) 628{ 629 AVPacket avpkt; 630 av_init_packet(&avpkt); 631 avpkt.data = buf; 632 avpkt.size = buf_size; 633 634 return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt); 635} 636#endif 637 638int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples, 639 int *frame_size_ptr, 640 AVPacket *avpkt) 641{ 642 int ret; 643 644 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){ 645 //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough 646 if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){ 647 av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n"); 648 return -1; 649 } 650 if(*frame_size_ptr < FF_MIN_BUFFER_SIZE || 651 *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){ 652 av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr); 653 return -1; 654 } 655 656 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt); 657 avctx->frame_number++; 658 }else{ 659 ret= 0; 660 *frame_size_ptr=0; 661 } 662 return ret; 663} 664 665#if LIBAVCODEC_VERSION_MAJOR < 53 666int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub, 667 int *got_sub_ptr, 668 const uint8_t *buf, int buf_size) 669{ 670 AVPacket avpkt; 671 av_init_packet(&avpkt); 672 avpkt.data = buf; 673 avpkt.size = buf_size; 674 675 return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt); 676} 677#endif 678 679int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, 680 int *got_sub_ptr, 681 AVPacket *avpkt) 682{ 683 int ret; 684 685 *got_sub_ptr = 0; 686 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt); 687 if (*got_sub_ptr) 688 avctx->frame_number++; 689 return ret; 690} 691 692av_cold int avcodec_close(AVCodecContext *avctx) 693{ 694 /* If there is a user-supplied mutex locking routine, call it. */ 695 if (ff_lockmgr_cb) { 696 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) 697 return -1; 698 } 699 700 entangled_thread_counter++; 701 if(entangled_thread_counter != 1){ 702 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n"); 703 entangled_thread_counter--; 704 return -1; 705 } 706 707 if (HAVE_THREADS && avctx->thread_opaque) 708 avcodec_thread_free(avctx); 709 if (avctx->codec && avctx->codec->close) 710 avctx->codec->close(avctx); 711 avcodec_default_free_buffers(avctx); 712 av_freep(&avctx->priv_data); 713 if(avctx->codec && avctx->codec->encode) 714 av_freep(&avctx->extradata); 715 avctx->codec = NULL; 716 entangled_thread_counter--; 717 718 /* Release any user-supplied mutex. */ 719 if (ff_lockmgr_cb) { 720 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); 721 } 722 return 0; 723} 724 725AVCodec *avcodec_find_encoder(enum CodecID id) 726{ 727 AVCodec *p, *experimental=NULL; 728 p = first_avcodec; 729 while (p) { 730 if (p->encode != NULL && p->id == id) { 731 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) { 732 experimental = p; 733 } else 734 return p; 735 } 736 p = p->next; 737 } 738 return experimental; 739} 740 741AVCodec *avcodec_find_encoder_by_name(const char *name) 742{ 743 AVCodec *p; 744 if (!name) 745 return NULL; 746 p = first_avcodec; 747 while (p) { 748 if (p->encode != NULL && strcmp(name,p->name) == 0) 749 return p; 750 p = p->next; 751 } 752 return NULL; 753} 754 755AVCodec *avcodec_find_decoder(enum CodecID id) 756{ 757 AVCodec *p; 758 p = first_avcodec; 759 while (p) { 760 if (p->decode != NULL && p->id == id) 761 return p; 762 p = p->next; 763 } 764 return NULL; 765} 766 767AVCodec *avcodec_find_decoder_by_name(const char *name) 768{ 769 AVCodec *p; 770 if (!name) 771 return NULL; 772 p = first_avcodec; 773 while (p) { 774 if (p->decode != NULL && strcmp(name,p->name) == 0) 775 return p; 776 p = p->next; 777 } 778 return NULL; 779} 780 781static int get_bit_rate(AVCodecContext *ctx) 782{ 783 int bit_rate; 784 int bits_per_sample; 785 786 switch(ctx->codec_type) { 787 case AVMEDIA_TYPE_VIDEO: 788 case AVMEDIA_TYPE_DATA: 789 case AVMEDIA_TYPE_SUBTITLE: 790 case AVMEDIA_TYPE_ATTACHMENT: 791 bit_rate = ctx->bit_rate; 792 break; 793 case AVMEDIA_TYPE_AUDIO: 794 bits_per_sample = av_get_bits_per_sample(ctx->codec_id); 795 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate; 796 break; 797 default: 798 bit_rate = 0; 799 break; 800 } 801 return bit_rate; 802} 803 804void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) 805{ 806 const char *codec_name; 807 AVCodec *p; 808 char buf1[32]; 809 int bitrate; 810 AVRational display_aspect_ratio; 811 812 if (encode) 813 p = avcodec_find_encoder(enc->codec_id); 814 else 815 p = avcodec_find_decoder(enc->codec_id); 816 817 if (p) { 818 codec_name = p->name; 819 } else if (enc->codec_id == CODEC_ID_MPEG2TS) { 820 /* fake mpeg2 transport stream codec (currently not 821 registered) */ 822 codec_name = "mpeg2ts"; 823 } else if (enc->codec_name[0] != '\0') { 824 codec_name = enc->codec_name; 825 } else { 826 /* output avi tags */ 827 if( isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF) 828 && isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){ 829 snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X", 830 enc->codec_tag & 0xff, 831 (enc->codec_tag >> 8) & 0xff, 832 (enc->codec_tag >> 16) & 0xff, 833 (enc->codec_tag >> 24) & 0xff, 834 enc->codec_tag); 835 } else { 836 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag); 837 } 838 codec_name = buf1; 839 } 840 841 switch(enc->codec_type) { 842 case AVMEDIA_TYPE_VIDEO: 843 snprintf(buf, buf_size, 844 "Video: %s%s", 845 codec_name, enc->mb_decision ? " (hq)" : ""); 846 if (enc->pix_fmt != PIX_FMT_NONE) { 847 snprintf(buf + strlen(buf), buf_size - strlen(buf), 848 ", %s", 849 avcodec_get_pix_fmt_name(enc->pix_fmt)); 850 } 851 if (enc->width) { 852 snprintf(buf + strlen(buf), buf_size - strlen(buf), 853 ", %dx%d", 854 enc->width, enc->height); 855 if (enc->sample_aspect_ratio.num) { 856 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, 857 enc->width*enc->sample_aspect_ratio.num, 858 enc->height*enc->sample_aspect_ratio.den, 859 1024*1024); 860 snprintf(buf + strlen(buf), buf_size - strlen(buf), 861 " [PAR %d:%d DAR %d:%d]", 862 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den, 863 display_aspect_ratio.num, display_aspect_ratio.den); 864 } 865 if(av_log_get_level() >= AV_LOG_DEBUG){ 866 int g= av_gcd(enc->time_base.num, enc->time_base.den); 867 snprintf(buf + strlen(buf), buf_size - strlen(buf), 868 ", %d/%d", 869 enc->time_base.num/g, enc->time_base.den/g); 870 } 871 } 872 if (encode) { 873 snprintf(buf + strlen(buf), buf_size - strlen(buf), 874 ", q=%d-%d", enc->qmin, enc->qmax); 875 } 876 break; 877 case AVMEDIA_TYPE_AUDIO: 878 snprintf(buf, buf_size, 879 "Audio: %s", 880 codec_name); 881 if (enc->sample_rate) { 882 snprintf(buf + strlen(buf), buf_size - strlen(buf), 883 ", %d Hz", enc->sample_rate); 884 } 885 av_strlcat(buf, ", ", buf_size); 886 avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout); 887 if (enc->sample_fmt != SAMPLE_FMT_NONE) { 888 snprintf(buf + strlen(buf), buf_size - strlen(buf), 889 ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt)); 890 } 891 break; 892 case AVMEDIA_TYPE_DATA: 893 snprintf(buf, buf_size, "Data: %s", codec_name); 894 break; 895 case AVMEDIA_TYPE_SUBTITLE: 896 snprintf(buf, buf_size, "Subtitle: %s", codec_name); 897 break; 898 case AVMEDIA_TYPE_ATTACHMENT: 899 snprintf(buf, buf_size, "Attachment: %s", codec_name); 900 break; 901 default: 902 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type); 903 return; 904 } 905 if (encode) { 906 if (enc->flags & CODEC_FLAG_PASS1) 907 snprintf(buf + strlen(buf), buf_size - strlen(buf), 908 ", pass 1"); 909 if (enc->flags & CODEC_FLAG_PASS2) 910 snprintf(buf + strlen(buf), buf_size - strlen(buf), 911 ", pass 2"); 912 } 913 bitrate = get_bit_rate(enc); 914 if (bitrate != 0) { 915 snprintf(buf + strlen(buf), buf_size - strlen(buf), 916 ", %d kb/s", bitrate / 1000); 917 } 918} 919 920unsigned avcodec_version( void ) 921{ 922 return LIBAVCODEC_VERSION_INT; 923} 924 925const char *avcodec_configuration(void) 926{ 927 return FFMPEG_CONFIGURATION; 928} 929 930const char *avcodec_license(void) 931{ 932#define LICENSE_PREFIX "libavcodec license: " 933 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1; 934} 935 936void avcodec_init(void) 937{ 938 static int initialized = 0; 939 940 if (initialized != 0) 941 return; 942 initialized = 1; 943 944 dsputil_static_init(); 945} 946 947void avcodec_flush_buffers(AVCodecContext *avctx) 948{ 949 if(avctx->codec->flush) 950 avctx->codec->flush(avctx); 951} 952 953void avcodec_default_free_buffers(AVCodecContext *s){ 954 int i, j; 955 956 if(s->internal_buffer==NULL) return; 957 958 if (s->internal_buffer_count) 959 av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count); 960 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){ 961 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i]; 962 for(j=0; j<4; j++){ 963 av_freep(&buf->base[j]); 964 buf->data[j]= NULL; 965 } 966 } 967 av_freep(&s->internal_buffer); 968 969 s->internal_buffer_count=0; 970} 971 972char av_get_pict_type_char(int pict_type){ 973 switch(pict_type){ 974 case FF_I_TYPE: return 'I'; 975 case FF_P_TYPE: return 'P'; 976 case FF_B_TYPE: return 'B'; 977 case FF_S_TYPE: return 'S'; 978 case FF_SI_TYPE:return 'i'; 979 case FF_SP_TYPE:return 'p'; 980 case FF_BI_TYPE:return 'b'; 981 default: return '?'; 982 } 983} 984 985int av_get_bits_per_sample(enum CodecID codec_id){ 986 switch(codec_id){ 987 case CODEC_ID_ADPCM_SBPRO_2: 988 return 2; 989 case CODEC_ID_ADPCM_SBPRO_3: 990 return 3; 991 case CODEC_ID_ADPCM_SBPRO_4: 992 case CODEC_ID_ADPCM_CT: 993 case CODEC_ID_ADPCM_IMA_WAV: 994 case CODEC_ID_ADPCM_MS: 995 case CODEC_ID_ADPCM_YAMAHA: 996 return 4; 997 case CODEC_ID_PCM_ALAW: 998 case CODEC_ID_PCM_MULAW: 999 case CODEC_ID_PCM_S8: 1000 case CODEC_ID_PCM_U8: 1001 case CODEC_ID_PCM_ZORK: 1002 return 8; 1003 case CODEC_ID_PCM_S16BE: 1004 case CODEC_ID_PCM_S16LE: 1005 case CODEC_ID_PCM_S16LE_PLANAR: 1006 case CODEC_ID_PCM_U16BE: 1007 case CODEC_ID_PCM_U16LE: 1008 return 16; 1009 case CODEC_ID_PCM_S24DAUD: 1010 case CODEC_ID_PCM_S24BE: 1011 case CODEC_ID_PCM_S24LE: 1012 case CODEC_ID_PCM_U24BE: 1013 case CODEC_ID_PCM_U24LE: 1014 return 24; 1015 case CODEC_ID_PCM_S32BE: 1016 case CODEC_ID_PCM_S32LE: 1017 case CODEC_ID_PCM_U32BE: 1018 case CODEC_ID_PCM_U32LE: 1019 case CODEC_ID_PCM_F32BE: 1020 case CODEC_ID_PCM_F32LE: 1021 return 32; 1022 case CODEC_ID_PCM_F64BE: 1023 case CODEC_ID_PCM_F64LE: 1024 return 64; 1025 default: 1026 return 0; 1027 } 1028} 1029 1030int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) { 1031 switch (sample_fmt) { 1032 case SAMPLE_FMT_U8: 1033 return 8; 1034 case SAMPLE_FMT_S16: 1035 return 16; 1036 case SAMPLE_FMT_S32: 1037 case SAMPLE_FMT_FLT: 1038 return 32; 1039 case SAMPLE_FMT_DBL: 1040 return 64; 1041 default: 1042 return 0; 1043 } 1044} 1045 1046#if !HAVE_THREADS 1047int avcodec_thread_init(AVCodecContext *s, int thread_count){ 1048 s->thread_count = thread_count; 1049 return -1; 1050} 1051#endif 1052 1053unsigned int av_xiphlacing(unsigned char *s, unsigned int v) 1054{ 1055 unsigned int n = 0; 1056 1057 while(v >= 0xff) { 1058 *s++ = 0xff; 1059 v -= 0xff; 1060 n++; 1061 } 1062 *s = v; 1063 n++; 1064 return n; 1065} 1066 1067/* Wrapper to work around the lack of mkstemp() on mingw/cygin. 1068 * Also, tries to create file in /tmp first, if possible. 1069 * *prefix can be a character constant; *filename will be allocated internally. 1070 * Returns file descriptor of opened file (or -1 on error) 1071 * and opened file name in **filename. */ 1072int av_tempfile(char *prefix, char **filename) { 1073 int fd=-1; 1074#if !HAVE_MKSTEMP 1075 *filename = tempnam(".", prefix); 1076#else 1077 size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */ 1078 *filename = av_malloc(len); 1079#endif 1080 /* -----common section-----*/ 1081 if (*filename == NULL) { 1082 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n"); 1083 return -1; 1084 } 1085#if !HAVE_MKSTEMP 1086 fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444); 1087#else 1088 snprintf(*filename, len, "/tmp/%sXXXXXX", prefix); 1089 fd = mkstemp(*filename); 1090 if (fd < 0) { 1091 snprintf(*filename, len, "./%sXXXXXX", prefix); 1092 fd = mkstemp(*filename); 1093 } 1094#endif 1095 /* -----common section-----*/ 1096 if (fd < 0) { 1097 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename); 1098 return -1; 1099 } 1100 return fd; /* success */ 1101} 1102 1103typedef struct { 1104 const char *abbr; 1105 int width, height; 1106} VideoFrameSizeAbbr; 1107 1108typedef struct { 1109 const char *abbr; 1110 int rate_num, rate_den; 1111} VideoFrameRateAbbr; 1112 1113static const VideoFrameSizeAbbr video_frame_size_abbrs[] = { 1114 { "ntsc", 720, 480 }, 1115 { "pal", 720, 576 }, 1116 { "qntsc", 352, 240 }, /* VCD compliant NTSC */ 1117 { "qpal", 352, 288 }, /* VCD compliant PAL */ 1118 { "sntsc", 640, 480 }, /* square pixel NTSC */ 1119 { "spal", 768, 576 }, /* square pixel PAL */ 1120 { "film", 352, 240 }, 1121 { "ntsc-film", 352, 240 }, 1122 { "sqcif", 128, 96 }, 1123 { "qcif", 176, 144 }, 1124 { "cif", 352, 288 }, 1125 { "4cif", 704, 576 }, 1126 { "16cif", 1408,1152 }, 1127 { "qqvga", 160, 120 }, 1128 { "qvga", 320, 240 }, 1129 { "vga", 640, 480 }, 1130 { "svga", 800, 600 }, 1131 { "xga", 1024, 768 }, 1132 { "uxga", 1600,1200 }, 1133 { "qxga", 2048,1536 }, 1134 { "sxga", 1280,1024 }, 1135 { "qsxga", 2560,2048 }, 1136 { "hsxga", 5120,4096 }, 1137 { "wvga", 852, 480 }, 1138 { "wxga", 1366, 768 }, 1139 { "wsxga", 1600,1024 }, 1140 { "wuxga", 1920,1200 }, 1141 { "woxga", 2560,1600 }, 1142 { "wqsxga", 3200,2048 }, 1143 { "wquxga", 3840,2400 }, 1144 { "whsxga", 6400,4096 }, 1145 { "whuxga", 7680,4800 }, 1146 { "cga", 320, 200 }, 1147 { "ega", 640, 350 }, 1148 { "hd480", 852, 480 }, 1149 { "hd720", 1280, 720 }, 1150 { "hd1080", 1920,1080 }, 1151}; 1152 1153static const VideoFrameRateAbbr video_frame_rate_abbrs[]= { 1154 { "ntsc", 30000, 1001 }, 1155 { "pal", 25, 1 }, 1156 { "qntsc", 30000, 1001 }, /* VCD compliant NTSC */ 1157 { "qpal", 25, 1 }, /* VCD compliant PAL */ 1158 { "sntsc", 30000, 1001 }, /* square pixel NTSC */ 1159 { "spal", 25, 1 }, /* square pixel PAL */ 1160 { "film", 24, 1 }, 1161 { "ntsc-film", 24000, 1001 }, 1162}; 1163 1164int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str) 1165{ 1166 int i; 1167 int n = FF_ARRAY_ELEMS(video_frame_size_abbrs); 1168 char *p; 1169 int frame_width = 0, frame_height = 0; 1170 1171 for(i=0;i<n;i++) { 1172 if (!strcmp(video_frame_size_abbrs[i].abbr, str)) { 1173 frame_width = video_frame_size_abbrs[i].width; 1174 frame_height = video_frame_size_abbrs[i].height; 1175 break; 1176 } 1177 } 1178 if (i == n) { 1179 p = str; 1180 frame_width = strtol(p, &p, 10); 1181 if (*p) 1182 p++; 1183 frame_height = strtol(p, &p, 10); 1184 } 1185 if (frame_width <= 0 || frame_height <= 0) 1186 return -1; 1187 *width_ptr = frame_width; 1188 *height_ptr = frame_height; 1189 return 0; 1190} 1191 1192int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg) 1193{ 1194 int i; 1195 int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs); 1196 char* cp; 1197 1198 /* First, we check our abbreviation table */ 1199 for (i = 0; i < n; ++i) 1200 if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) { 1201 frame_rate->num = video_frame_rate_abbrs[i].rate_num; 1202 frame_rate->den = video_frame_rate_abbrs[i].rate_den; 1203 return 0; 1204 } 1205 1206 /* Then, we try to parse it as fraction */ 1207 cp = strchr(arg, '/'); 1208 if (!cp) 1209 cp = strchr(arg, ':'); 1210 if (cp) { 1211 char* cpp; 1212 frame_rate->num = strtol(arg, &cpp, 10); 1213 if (cpp != arg || cpp == cp) 1214 frame_rate->den = strtol(cp+1, &cpp, 10); 1215 else 1216 frame_rate->num = 0; 1217 } 1218 else { 1219 /* Finally we give up and parse it as double */ 1220 AVRational time_base = av_d2q(strtod(arg, 0), 1001000); 1221 frame_rate->den = time_base.den; 1222 frame_rate->num = time_base.num; 1223 } 1224 if (!frame_rate->num || !frame_rate->den) 1225 return -1; 1226 else 1227 return 0; 1228} 1229 1230int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){ 1231 int i; 1232 for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++); 1233 return i; 1234} 1235 1236void av_log_missing_feature(void *avc, const char *feature, int want_sample) 1237{ 1238 av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg " 1239 "version to the newest one from SVN. If the problem still " 1240 "occurs, it means that your file has a feature which has not " 1241 "been implemented.", feature); 1242 if(want_sample) 1243 av_log_ask_for_sample(avc, NULL); 1244 else 1245 av_log(avc, AV_LOG_WARNING, "\n"); 1246} 1247 1248void av_log_ask_for_sample(void *avc, const char *msg) 1249{ 1250 if (msg) 1251 av_log(avc, AV_LOG_WARNING, "%s ", msg); 1252 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample " 1253 "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ " 1254 "and contact the ffmpeg-devel mailing list.\n"); 1255} 1256 1257static AVHWAccel *first_hwaccel = NULL; 1258 1259void av_register_hwaccel(AVHWAccel *hwaccel) 1260{ 1261 AVHWAccel **p = &first_hwaccel; 1262 while (*p) 1263 p = &(*p)->next; 1264 *p = hwaccel; 1265 hwaccel->next = NULL; 1266} 1267 1268AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel) 1269{ 1270 return hwaccel ? hwaccel->next : first_hwaccel; 1271} 1272 1273AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt) 1274{ 1275 AVHWAccel *hwaccel=NULL; 1276 1277 while((hwaccel= av_hwaccel_next(hwaccel))){ 1278 if ( hwaccel->id == codec_id 1279 && hwaccel->pix_fmt == pix_fmt) 1280 return hwaccel; 1281 } 1282 return NULL; 1283} 1284 1285int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op)) 1286{ 1287 if (ff_lockmgr_cb) { 1288 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY)) 1289 return -1; 1290 } 1291 1292 ff_lockmgr_cb = cb; 1293 1294 if (ff_lockmgr_cb) { 1295 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE)) 1296 return -1; 1297 } 1298 return 0; 1299} 1300