You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1187 lines
34KB

  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. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavcore/imgutils.h"
  31. #include "libavcore/samplefmt.h"
  32. #include "avcodec.h"
  33. #include "dsputil.h"
  34. #include "libavutil/opt.h"
  35. #include "imgconvert.h"
  36. #include "audioconvert.h"
  37. #include "internal.h"
  38. #include <stdlib.h>
  39. #include <stdarg.h>
  40. #include <limits.h>
  41. #include <float.h>
  42. static int volatile entangled_thread_counter=0;
  43. int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  44. static void *codec_mutex;
  45. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  46. {
  47. if(min_size < *size)
  48. return ptr;
  49. *size= FFMAX(17*min_size/16 + 32, min_size);
  50. ptr= av_realloc(ptr, *size);
  51. 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
  52. *size= 0;
  53. return ptr;
  54. }
  55. void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size)
  56. {
  57. void **p = ptr;
  58. if (min_size < *size)
  59. return;
  60. *size= FFMAX(17*min_size/16 + 32, min_size);
  61. av_free(*p);
  62. *p = av_malloc(*size);
  63. if (!*p) *size = 0;
  64. }
  65. /* encoder management */
  66. static AVCodec *first_avcodec = NULL;
  67. AVCodec *av_codec_next(AVCodec *c){
  68. if(c) return c->next;
  69. else return first_avcodec;
  70. }
  71. void avcodec_register(AVCodec *codec)
  72. {
  73. AVCodec **p;
  74. avcodec_init();
  75. p = &first_avcodec;
  76. while (*p != NULL) p = &(*p)->next;
  77. *p = codec;
  78. codec->next = NULL;
  79. }
  80. #if LIBAVCODEC_VERSION_MAJOR < 53
  81. void register_avcodec(AVCodec *codec)
  82. {
  83. avcodec_register(codec);
  84. }
  85. #endif
  86. unsigned avcodec_get_edge_width(void)
  87. {
  88. return EDGE_WIDTH;
  89. }
  90. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  91. s->coded_width = width;
  92. s->coded_height= height;
  93. s->width = -((-width )>>s->lowres);
  94. s->height= -((-height)>>s->lowres);
  95. }
  96. typedef struct InternalBuffer{
  97. int last_pic_num;
  98. uint8_t *base[4];
  99. uint8_t *data[4];
  100. int linesize[4];
  101. int width, height;
  102. enum PixelFormat pix_fmt;
  103. }InternalBuffer;
  104. #define INTERNAL_BUFFER_SIZE 32
  105. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  106. int w_align= 1;
  107. int h_align= 1;
  108. switch(s->pix_fmt){
  109. case PIX_FMT_YUV420P:
  110. case PIX_FMT_YUYV422:
  111. case PIX_FMT_UYVY422:
  112. case PIX_FMT_YUV422P:
  113. case PIX_FMT_YUV440P:
  114. case PIX_FMT_YUV444P:
  115. case PIX_FMT_GRAY8:
  116. case PIX_FMT_GRAY16BE:
  117. case PIX_FMT_GRAY16LE:
  118. case PIX_FMT_YUVJ420P:
  119. case PIX_FMT_YUVJ422P:
  120. case PIX_FMT_YUVJ440P:
  121. case PIX_FMT_YUVJ444P:
  122. case PIX_FMT_YUVA420P:
  123. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  124. h_align= 16;
  125. 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)
  126. h_align= 32; // interlaced is rounded up to 2 MBs
  127. break;
  128. case PIX_FMT_YUV411P:
  129. case PIX_FMT_UYYVYY411:
  130. w_align=32;
  131. h_align=8;
  132. break;
  133. case PIX_FMT_YUV410P:
  134. if(s->codec_id == CODEC_ID_SVQ1){
  135. w_align=64;
  136. h_align=64;
  137. }
  138. case PIX_FMT_RGB555:
  139. if(s->codec_id == CODEC_ID_RPZA){
  140. w_align=4;
  141. h_align=4;
  142. }
  143. case PIX_FMT_PAL8:
  144. case PIX_FMT_BGR8:
  145. case PIX_FMT_RGB8:
  146. if(s->codec_id == CODEC_ID_SMC){
  147. w_align=4;
  148. h_align=4;
  149. }
  150. break;
  151. case PIX_FMT_BGR24:
  152. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  153. w_align=4;
  154. h_align=4;
  155. }
  156. break;
  157. default:
  158. w_align= 1;
  159. h_align= 1;
  160. break;
  161. }
  162. *width = FFALIGN(*width , w_align);
  163. *height= FFALIGN(*height, h_align);
  164. if(s->codec_id == CODEC_ID_H264)
  165. *height+=2; // some of the optimized chroma MC reads one line too much
  166. linesize_align[0] =
  167. linesize_align[1] =
  168. linesize_align[2] =
  169. linesize_align[3] = STRIDE_ALIGN;
  170. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  171. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  172. //picture size unneccessarily in some cases. The solution here is not
  173. //pretty and better ideas are welcome!
  174. #if HAVE_MMX
  175. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  176. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  177. s->codec_id == CODEC_ID_VP6A) {
  178. linesize_align[0] =
  179. linesize_align[1] =
  180. linesize_align[2] = 16;
  181. }
  182. #endif
  183. }
  184. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  185. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  186. int linesize_align[4];
  187. int align;
  188. avcodec_align_dimensions2(s, width, height, linesize_align);
  189. align = FFMAX(linesize_align[0], linesize_align[3]);
  190. linesize_align[1] <<= chroma_shift;
  191. linesize_align[2] <<= chroma_shift;
  192. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  193. *width=FFALIGN(*width, align);
  194. }
  195. #if LIBAVCODEC_VERSION_MAJOR < 53
  196. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  197. return av_image_check_size(w, h, 0, av_log_ctx);
  198. }
  199. #endif
  200. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  201. int i;
  202. int w= s->width;
  203. int h= s->height;
  204. InternalBuffer *buf;
  205. int *picture_number;
  206. if(pic->data[0]!=NULL) {
  207. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  208. return -1;
  209. }
  210. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  211. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  212. return -1;
  213. }
  214. if(av_image_check_size(w, h, 0, s))
  215. return -1;
  216. if(s->internal_buffer==NULL){
  217. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  218. }
  219. #if 0
  220. s->internal_buffer= av_fast_realloc(
  221. s->internal_buffer,
  222. &s->internal_buffer_size,
  223. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  224. );
  225. #endif
  226. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  227. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  228. (*picture_number)++;
  229. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  230. for(i=0; i<4; i++){
  231. av_freep(&buf->base[i]);
  232. buf->data[i]= NULL;
  233. }
  234. }
  235. if(buf->base[0]){
  236. pic->age= *picture_number - buf->last_pic_num;
  237. buf->last_pic_num= *picture_number;
  238. }else{
  239. int h_chroma_shift, v_chroma_shift;
  240. int size[4] = {0};
  241. int tmpsize;
  242. int unaligned;
  243. AVPicture picture;
  244. int stride_align[4];
  245. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  246. avcodec_align_dimensions2(s, &w, &h, stride_align);
  247. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  248. w+= EDGE_WIDTH*2;
  249. h+= EDGE_WIDTH*2;
  250. }
  251. do {
  252. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  253. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  254. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  255. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  256. w += w & ~(w-1);
  257. unaligned = 0;
  258. for (i=0; i<4; i++){
  259. unaligned |= picture.linesize[i] % stride_align[i];
  260. }
  261. } while (unaligned);
  262. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  263. if (tmpsize < 0)
  264. return -1;
  265. for (i=0; i<3 && picture.data[i+1]; i++)
  266. size[i] = picture.data[i+1] - picture.data[i];
  267. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  268. buf->last_pic_num= -256*256*256*64;
  269. memset(buf->base, 0, sizeof(buf->base));
  270. memset(buf->data, 0, sizeof(buf->data));
  271. for(i=0; i<4 && size[i]; i++){
  272. const int h_shift= i==0 ? 0 : h_chroma_shift;
  273. const int v_shift= i==0 ? 0 : v_chroma_shift;
  274. buf->linesize[i]= picture.linesize[i];
  275. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  276. if(buf->base[i]==NULL) return -1;
  277. memset(buf->base[i], 128, size[i]);
  278. // no edge if EDGE EMU or not planar YUV
  279. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  280. buf->data[i] = buf->base[i];
  281. else
  282. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  283. }
  284. if(size[1] && !size[2])
  285. ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
  286. buf->width = s->width;
  287. buf->height = s->height;
  288. buf->pix_fmt= s->pix_fmt;
  289. pic->age= 256*256*256*64;
  290. }
  291. pic->type= FF_BUFFER_TYPE_INTERNAL;
  292. for(i=0; i<4; i++){
  293. pic->base[i]= buf->base[i];
  294. pic->data[i]= buf->data[i];
  295. pic->linesize[i]= buf->linesize[i];
  296. }
  297. s->internal_buffer_count++;
  298. pic->reordered_opaque= s->reordered_opaque;
  299. if(s->debug&FF_DEBUG_BUFFERS)
  300. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  301. return 0;
  302. }
  303. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  304. int i;
  305. InternalBuffer *buf, *last;
  306. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  307. assert(s->internal_buffer_count);
  308. buf = NULL; /* avoids warning */
  309. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  310. buf= &((InternalBuffer*)s->internal_buffer)[i];
  311. if(buf->data[0] == pic->data[0])
  312. break;
  313. }
  314. assert(i < s->internal_buffer_count);
  315. s->internal_buffer_count--;
  316. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  317. FFSWAP(InternalBuffer, *buf, *last);
  318. for(i=0; i<4; i++){
  319. pic->data[i]=NULL;
  320. // pic->base[i]=NULL;
  321. }
  322. //printf("R%X\n", pic->opaque);
  323. if(s->debug&FF_DEBUG_BUFFERS)
  324. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  325. }
  326. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  327. AVFrame temp_pic;
  328. int i;
  329. /* If no picture return a new buffer */
  330. if(pic->data[0] == NULL) {
  331. /* We will copy from buffer, so must be readable */
  332. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  333. return s->get_buffer(s, pic);
  334. }
  335. /* If internal buffer type return the same buffer */
  336. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  337. pic->reordered_opaque= s->reordered_opaque;
  338. return 0;
  339. }
  340. /*
  341. * Not internal type and reget_buffer not overridden, emulate cr buffer
  342. */
  343. temp_pic = *pic;
  344. for(i = 0; i < 4; i++)
  345. pic->data[i] = pic->base[i] = NULL;
  346. pic->opaque = NULL;
  347. /* Allocate new frame */
  348. if (s->get_buffer(s, pic))
  349. return -1;
  350. /* Copy image data from old buffer to new buffer */
  351. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  352. s->height);
  353. s->release_buffer(s, &temp_pic); // Release old frame
  354. return 0;
  355. }
  356. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  357. int i;
  358. for(i=0; i<count; i++){
  359. int r= func(c, (char*)arg + i*size);
  360. if(ret) ret[i]= r;
  361. }
  362. return 0;
  363. }
  364. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  365. int i;
  366. for(i=0; i<count; i++){
  367. int r= func(c, arg, i, 0);
  368. if(ret) ret[i]= r;
  369. }
  370. return 0;
  371. }
  372. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  373. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  374. ++fmt;
  375. return fmt[0];
  376. }
  377. void avcodec_get_frame_defaults(AVFrame *pic){
  378. memset(pic, 0, sizeof(AVFrame));
  379. pic->pts= AV_NOPTS_VALUE;
  380. pic->key_frame= 1;
  381. }
  382. AVFrame *avcodec_alloc_frame(void){
  383. AVFrame *pic= av_malloc(sizeof(AVFrame));
  384. if(pic==NULL) return NULL;
  385. avcodec_get_frame_defaults(pic);
  386. return pic;
  387. }
  388. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  389. {
  390. int ret= -1;
  391. /* If there is a user-supplied mutex locking routine, call it. */
  392. if (ff_lockmgr_cb) {
  393. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  394. return -1;
  395. }
  396. entangled_thread_counter++;
  397. if(entangled_thread_counter != 1){
  398. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  399. goto end;
  400. }
  401. if(avctx->codec || !codec)
  402. goto end;
  403. if (codec->priv_data_size > 0) {
  404. if(!avctx->priv_data){
  405. avctx->priv_data = av_mallocz(codec->priv_data_size);
  406. if (!avctx->priv_data) {
  407. ret = AVERROR(ENOMEM);
  408. goto end;
  409. }
  410. if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
  411. *(AVClass**)avctx->priv_data= codec->priv_class;
  412. av_opt_set_defaults(avctx->priv_data);
  413. }
  414. }
  415. } else {
  416. avctx->priv_data = NULL;
  417. }
  418. if(avctx->coded_width && avctx->coded_height)
  419. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  420. else if(avctx->width && avctx->height)
  421. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  422. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  423. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  424. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  425. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  426. avcodec_set_dimensions(avctx, 0, 0);
  427. }
  428. #define SANE_NB_CHANNELS 128U
  429. if (avctx->channels > SANE_NB_CHANNELS) {
  430. ret = AVERROR(EINVAL);
  431. goto free_and_end;
  432. }
  433. avctx->codec = codec;
  434. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  435. avctx->codec_id == CODEC_ID_NONE) {
  436. avctx->codec_type = codec->type;
  437. avctx->codec_id = codec->id;
  438. }
  439. if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
  440. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  441. goto free_and_end;
  442. }
  443. avctx->frame_number = 0;
  444. if (avctx->codec->max_lowres < avctx->lowres) {
  445. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  446. avctx->codec->max_lowres);
  447. goto free_and_end;
  448. }
  449. if(avctx->codec->init){
  450. ret = avctx->codec->init(avctx);
  451. if (ret < 0) {
  452. goto free_and_end;
  453. }
  454. }
  455. ret=0;
  456. end:
  457. entangled_thread_counter--;
  458. /* Release any user-supplied mutex. */
  459. if (ff_lockmgr_cb) {
  460. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  461. }
  462. return ret;
  463. free_and_end:
  464. av_freep(&avctx->priv_data);
  465. avctx->codec= NULL;
  466. goto end;
  467. }
  468. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  469. const short *samples)
  470. {
  471. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  472. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  473. return -1;
  474. }
  475. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  476. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  477. avctx->frame_number++;
  478. return ret;
  479. }else
  480. return 0;
  481. }
  482. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  483. const AVFrame *pict)
  484. {
  485. if(buf_size < FF_MIN_BUFFER_SIZE){
  486. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  487. return -1;
  488. }
  489. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  490. return -1;
  491. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  492. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  493. avctx->frame_number++;
  494. emms_c(); //needed to avoid an emms_c() call before every return;
  495. return ret;
  496. }else
  497. return 0;
  498. }
  499. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  500. const AVSubtitle *sub)
  501. {
  502. int ret;
  503. if(sub->start_display_time) {
  504. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  505. return -1;
  506. }
  507. if(sub->num_rects == 0 || !sub->rects)
  508. return -1;
  509. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  510. avctx->frame_number++;
  511. return ret;
  512. }
  513. #if FF_API_VIDEO_OLD
  514. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  515. int *got_picture_ptr,
  516. const uint8_t *buf, int buf_size)
  517. {
  518. AVPacket avpkt;
  519. av_init_packet(&avpkt);
  520. avpkt.data = buf;
  521. avpkt.size = buf_size;
  522. // HACK for CorePNG to decode as normal PNG by default
  523. avpkt.flags = AV_PKT_FLAG_KEY;
  524. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  525. }
  526. #endif
  527. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  528. int *got_picture_ptr,
  529. AVPacket *avpkt)
  530. {
  531. int ret;
  532. *got_picture_ptr= 0;
  533. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  534. return -1;
  535. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  536. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  537. avpkt);
  538. emms_c(); //needed to avoid an emms_c() call before every return;
  539. if (*got_picture_ptr)
  540. avctx->frame_number++;
  541. }else
  542. ret= 0;
  543. return ret;
  544. }
  545. #if FF_API_AUDIO_OLD
  546. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  547. int *frame_size_ptr,
  548. const uint8_t *buf, int buf_size)
  549. {
  550. AVPacket avpkt;
  551. av_init_packet(&avpkt);
  552. avpkt.data = buf;
  553. avpkt.size = buf_size;
  554. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  555. }
  556. #endif
  557. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  558. int *frame_size_ptr,
  559. AVPacket *avpkt)
  560. {
  561. int ret;
  562. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  563. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  564. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  565. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  566. return -1;
  567. }
  568. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  569. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  570. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  571. return -1;
  572. }
  573. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  574. avctx->frame_number++;
  575. }else{
  576. ret= 0;
  577. *frame_size_ptr=0;
  578. }
  579. return ret;
  580. }
  581. #if FF_API_SUBTITLE_OLD
  582. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  583. int *got_sub_ptr,
  584. const uint8_t *buf, int buf_size)
  585. {
  586. AVPacket avpkt;
  587. av_init_packet(&avpkt);
  588. avpkt.data = buf;
  589. avpkt.size = buf_size;
  590. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  591. }
  592. #endif
  593. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  594. int *got_sub_ptr,
  595. AVPacket *avpkt)
  596. {
  597. int ret;
  598. *got_sub_ptr = 0;
  599. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  600. if (*got_sub_ptr)
  601. avctx->frame_number++;
  602. return ret;
  603. }
  604. void avsubtitle_free(AVSubtitle *sub)
  605. {
  606. int i;
  607. for (i = 0; i < sub->num_rects; i++)
  608. {
  609. av_freep(&sub->rects[i]->pict.data[0]);
  610. av_freep(&sub->rects[i]->pict.data[1]);
  611. av_freep(&sub->rects[i]->pict.data[2]);
  612. av_freep(&sub->rects[i]->pict.data[3]);
  613. av_freep(&sub->rects[i]->text);
  614. av_freep(&sub->rects[i]->ass);
  615. av_freep(&sub->rects[i]);
  616. }
  617. av_freep(&sub->rects);
  618. memset(sub, 0, sizeof(AVSubtitle));
  619. }
  620. av_cold int avcodec_close(AVCodecContext *avctx)
  621. {
  622. /* If there is a user-supplied mutex locking routine, call it. */
  623. if (ff_lockmgr_cb) {
  624. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  625. return -1;
  626. }
  627. entangled_thread_counter++;
  628. if(entangled_thread_counter != 1){
  629. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  630. entangled_thread_counter--;
  631. return -1;
  632. }
  633. if (HAVE_THREADS && avctx->thread_opaque)
  634. avcodec_thread_free(avctx);
  635. if (avctx->codec && avctx->codec->close)
  636. avctx->codec->close(avctx);
  637. avcodec_default_free_buffers(avctx);
  638. avctx->coded_frame = NULL;
  639. av_freep(&avctx->priv_data);
  640. if(avctx->codec && avctx->codec->encode)
  641. av_freep(&avctx->extradata);
  642. avctx->codec = NULL;
  643. entangled_thread_counter--;
  644. /* Release any user-supplied mutex. */
  645. if (ff_lockmgr_cb) {
  646. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  647. }
  648. return 0;
  649. }
  650. AVCodec *avcodec_find_encoder(enum CodecID id)
  651. {
  652. AVCodec *p, *experimental=NULL;
  653. p = first_avcodec;
  654. while (p) {
  655. if (p->encode != NULL && p->id == id) {
  656. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  657. experimental = p;
  658. } else
  659. return p;
  660. }
  661. p = p->next;
  662. }
  663. return experimental;
  664. }
  665. AVCodec *avcodec_find_encoder_by_name(const char *name)
  666. {
  667. AVCodec *p;
  668. if (!name)
  669. return NULL;
  670. p = first_avcodec;
  671. while (p) {
  672. if (p->encode != NULL && strcmp(name,p->name) == 0)
  673. return p;
  674. p = p->next;
  675. }
  676. return NULL;
  677. }
  678. AVCodec *avcodec_find_decoder(enum CodecID id)
  679. {
  680. AVCodec *p;
  681. p = first_avcodec;
  682. while (p) {
  683. if (p->decode != NULL && p->id == id)
  684. return p;
  685. p = p->next;
  686. }
  687. return NULL;
  688. }
  689. AVCodec *avcodec_find_decoder_by_name(const char *name)
  690. {
  691. AVCodec *p;
  692. if (!name)
  693. return NULL;
  694. p = first_avcodec;
  695. while (p) {
  696. if (p->decode != NULL && strcmp(name,p->name) == 0)
  697. return p;
  698. p = p->next;
  699. }
  700. return NULL;
  701. }
  702. static int get_bit_rate(AVCodecContext *ctx)
  703. {
  704. int bit_rate;
  705. int bits_per_sample;
  706. switch(ctx->codec_type) {
  707. case AVMEDIA_TYPE_VIDEO:
  708. case AVMEDIA_TYPE_DATA:
  709. case AVMEDIA_TYPE_SUBTITLE:
  710. case AVMEDIA_TYPE_ATTACHMENT:
  711. bit_rate = ctx->bit_rate;
  712. break;
  713. case AVMEDIA_TYPE_AUDIO:
  714. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  715. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  716. break;
  717. default:
  718. bit_rate = 0;
  719. break;
  720. }
  721. return bit_rate;
  722. }
  723. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  724. {
  725. int i, len, ret = 0;
  726. for (i = 0; i < 4; i++) {
  727. len = snprintf(buf, buf_size,
  728. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  729. buf += len;
  730. buf_size = buf_size > len ? buf_size - len : 0;
  731. ret += len;
  732. codec_tag>>=8;
  733. }
  734. return ret;
  735. }
  736. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  737. {
  738. const char *codec_name;
  739. AVCodec *p;
  740. char buf1[32];
  741. int bitrate;
  742. AVRational display_aspect_ratio;
  743. if (encode)
  744. p = avcodec_find_encoder(enc->codec_id);
  745. else
  746. p = avcodec_find_decoder(enc->codec_id);
  747. if (p) {
  748. codec_name = p->name;
  749. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  750. /* fake mpeg2 transport stream codec (currently not
  751. registered) */
  752. codec_name = "mpeg2ts";
  753. } else if (enc->codec_name[0] != '\0') {
  754. codec_name = enc->codec_name;
  755. } else {
  756. /* output avi tags */
  757. char tag_buf[32];
  758. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  759. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  760. codec_name = buf1;
  761. }
  762. switch(enc->codec_type) {
  763. case AVMEDIA_TYPE_VIDEO:
  764. snprintf(buf, buf_size,
  765. "Video: %s%s",
  766. codec_name, enc->mb_decision ? " (hq)" : "");
  767. if (enc->pix_fmt != PIX_FMT_NONE) {
  768. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  769. ", %s",
  770. avcodec_get_pix_fmt_name(enc->pix_fmt));
  771. }
  772. if (enc->width) {
  773. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  774. ", %dx%d",
  775. enc->width, enc->height);
  776. if (enc->sample_aspect_ratio.num) {
  777. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  778. enc->width*enc->sample_aspect_ratio.num,
  779. enc->height*enc->sample_aspect_ratio.den,
  780. 1024*1024);
  781. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  782. " [PAR %d:%d DAR %d:%d]",
  783. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  784. display_aspect_ratio.num, display_aspect_ratio.den);
  785. }
  786. if(av_log_get_level() >= AV_LOG_DEBUG){
  787. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  788. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  789. ", %d/%d",
  790. enc->time_base.num/g, enc->time_base.den/g);
  791. }
  792. }
  793. if (encode) {
  794. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  795. ", q=%d-%d", enc->qmin, enc->qmax);
  796. }
  797. break;
  798. case AVMEDIA_TYPE_AUDIO:
  799. snprintf(buf, buf_size,
  800. "Audio: %s",
  801. codec_name);
  802. if (enc->sample_rate) {
  803. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  804. ", %d Hz", enc->sample_rate);
  805. }
  806. av_strlcat(buf, ", ", buf_size);
  807. avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  808. if (enc->sample_fmt != SAMPLE_FMT_NONE) {
  809. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  810. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  811. }
  812. break;
  813. case AVMEDIA_TYPE_DATA:
  814. snprintf(buf, buf_size, "Data: %s", codec_name);
  815. break;
  816. case AVMEDIA_TYPE_SUBTITLE:
  817. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  818. break;
  819. case AVMEDIA_TYPE_ATTACHMENT:
  820. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  821. break;
  822. default:
  823. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  824. return;
  825. }
  826. if (encode) {
  827. if (enc->flags & CODEC_FLAG_PASS1)
  828. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  829. ", pass 1");
  830. if (enc->flags & CODEC_FLAG_PASS2)
  831. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  832. ", pass 2");
  833. }
  834. bitrate = get_bit_rate(enc);
  835. if (bitrate != 0) {
  836. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  837. ", %d kb/s", bitrate / 1000);
  838. }
  839. }
  840. unsigned avcodec_version( void )
  841. {
  842. return LIBAVCODEC_VERSION_INT;
  843. }
  844. const char *avcodec_configuration(void)
  845. {
  846. return FFMPEG_CONFIGURATION;
  847. }
  848. const char *avcodec_license(void)
  849. {
  850. #define LICENSE_PREFIX "libavcodec license: "
  851. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  852. }
  853. void avcodec_init(void)
  854. {
  855. static int initialized = 0;
  856. if (initialized != 0)
  857. return;
  858. initialized = 1;
  859. dsputil_static_init();
  860. }
  861. void avcodec_flush_buffers(AVCodecContext *avctx)
  862. {
  863. if(avctx->codec->flush)
  864. avctx->codec->flush(avctx);
  865. }
  866. void avcodec_default_free_buffers(AVCodecContext *s){
  867. int i, j;
  868. if(s->internal_buffer==NULL) return;
  869. if (s->internal_buffer_count)
  870. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  871. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  872. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  873. for(j=0; j<4; j++){
  874. av_freep(&buf->base[j]);
  875. buf->data[j]= NULL;
  876. }
  877. }
  878. av_freep(&s->internal_buffer);
  879. s->internal_buffer_count=0;
  880. }
  881. char av_get_pict_type_char(int pict_type){
  882. switch(pict_type){
  883. case FF_I_TYPE: return 'I';
  884. case FF_P_TYPE: return 'P';
  885. case FF_B_TYPE: return 'B';
  886. case FF_S_TYPE: return 'S';
  887. case FF_SI_TYPE:return 'i';
  888. case FF_SP_TYPE:return 'p';
  889. case FF_BI_TYPE:return 'b';
  890. default: return '?';
  891. }
  892. }
  893. int av_get_bits_per_sample(enum CodecID codec_id){
  894. switch(codec_id){
  895. case CODEC_ID_ADPCM_SBPRO_2:
  896. return 2;
  897. case CODEC_ID_ADPCM_SBPRO_3:
  898. return 3;
  899. case CODEC_ID_ADPCM_SBPRO_4:
  900. case CODEC_ID_ADPCM_CT:
  901. case CODEC_ID_ADPCM_IMA_WAV:
  902. case CODEC_ID_ADPCM_MS:
  903. case CODEC_ID_ADPCM_YAMAHA:
  904. return 4;
  905. case CODEC_ID_PCM_ALAW:
  906. case CODEC_ID_PCM_MULAW:
  907. case CODEC_ID_PCM_S8:
  908. case CODEC_ID_PCM_U8:
  909. case CODEC_ID_PCM_ZORK:
  910. return 8;
  911. case CODEC_ID_PCM_S16BE:
  912. case CODEC_ID_PCM_S16LE:
  913. case CODEC_ID_PCM_S16LE_PLANAR:
  914. case CODEC_ID_PCM_U16BE:
  915. case CODEC_ID_PCM_U16LE:
  916. return 16;
  917. case CODEC_ID_PCM_S24DAUD:
  918. case CODEC_ID_PCM_S24BE:
  919. case CODEC_ID_PCM_S24LE:
  920. case CODEC_ID_PCM_U24BE:
  921. case CODEC_ID_PCM_U24LE:
  922. return 24;
  923. case CODEC_ID_PCM_S32BE:
  924. case CODEC_ID_PCM_S32LE:
  925. case CODEC_ID_PCM_U32BE:
  926. case CODEC_ID_PCM_U32LE:
  927. case CODEC_ID_PCM_F32BE:
  928. case CODEC_ID_PCM_F32LE:
  929. return 32;
  930. case CODEC_ID_PCM_F64BE:
  931. case CODEC_ID_PCM_F64LE:
  932. return 64;
  933. default:
  934. return 0;
  935. }
  936. }
  937. #if FF_API_OLD_SAMPLE_FMT
  938. int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
  939. return av_get_bits_per_sample_fmt(sample_fmt);
  940. }
  941. #endif
  942. #if !HAVE_THREADS
  943. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  944. s->thread_count = thread_count;
  945. return -1;
  946. }
  947. #endif
  948. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  949. {
  950. unsigned int n = 0;
  951. while(v >= 0xff) {
  952. *s++ = 0xff;
  953. v -= 0xff;
  954. n++;
  955. }
  956. *s = v;
  957. n++;
  958. return n;
  959. }
  960. #if LIBAVCODEC_VERSION_MAJOR < 53
  961. #include "libavcore/parseutils.h"
  962. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  963. {
  964. return av_parse_video_size(width_ptr, height_ptr, str);
  965. }
  966. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  967. {
  968. return av_parse_video_rate(frame_rate, arg);
  969. }
  970. #endif
  971. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  972. int i;
  973. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  974. return i;
  975. }
  976. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  977. {
  978. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  979. "version to the newest one from SVN. If the problem still "
  980. "occurs, it means that your file has a feature which has not "
  981. "been implemented.", feature);
  982. if(want_sample)
  983. av_log_ask_for_sample(avc, NULL);
  984. else
  985. av_log(avc, AV_LOG_WARNING, "\n");
  986. }
  987. void av_log_ask_for_sample(void *avc, const char *msg)
  988. {
  989. if (msg)
  990. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  991. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  992. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  993. "and contact the ffmpeg-devel mailing list.\n");
  994. }
  995. static AVHWAccel *first_hwaccel = NULL;
  996. void av_register_hwaccel(AVHWAccel *hwaccel)
  997. {
  998. AVHWAccel **p = &first_hwaccel;
  999. while (*p)
  1000. p = &(*p)->next;
  1001. *p = hwaccel;
  1002. hwaccel->next = NULL;
  1003. }
  1004. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1005. {
  1006. return hwaccel ? hwaccel->next : first_hwaccel;
  1007. }
  1008. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1009. {
  1010. AVHWAccel *hwaccel=NULL;
  1011. while((hwaccel= av_hwaccel_next(hwaccel))){
  1012. if ( hwaccel->id == codec_id
  1013. && hwaccel->pix_fmt == pix_fmt)
  1014. return hwaccel;
  1015. }
  1016. return NULL;
  1017. }
  1018. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1019. {
  1020. if (ff_lockmgr_cb) {
  1021. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1022. return -1;
  1023. }
  1024. ff_lockmgr_cb = cb;
  1025. if (ff_lockmgr_cb) {
  1026. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1027. return -1;
  1028. }
  1029. return 0;
  1030. }
  1031. unsigned int ff_toupper4(unsigned int x)
  1032. {
  1033. return toupper( x &0xFF)
  1034. + (toupper((x>>8 )&0xFF)<<8 )
  1035. + (toupper((x>>16)&0xFF)<<16)
  1036. + (toupper((x>>24)&0xFF)<<24);
  1037. }