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.

1863 lines
57KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "avcodec.h"
  36. #include "dsputil.h"
  37. #include "libavutil/opt.h"
  38. #include "imgconvert.h"
  39. #include "thread.h"
  40. #include "audioconvert.h"
  41. #include "internal.h"
  42. #include "bytestream.h"
  43. #include <stdlib.h>
  44. #include <stdarg.h>
  45. #include <limits.h>
  46. #include <float.h>
  47. static int volatile entangled_thread_counter=0;
  48. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  49. static void *codec_mutex;
  50. static void *avformat_mutex;
  51. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  52. {
  53. if(min_size < *size)
  54. return ptr;
  55. min_size= FFMAX(17*min_size/16 + 32, min_size);
  56. ptr= av_realloc(ptr, min_size);
  57. 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
  58. min_size= 0;
  59. *size= min_size;
  60. return ptr;
  61. }
  62. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  63. {
  64. void **p = ptr;
  65. if (min_size < *size)
  66. return;
  67. min_size= FFMAX(17*min_size/16 + 32, min_size);
  68. av_free(*p);
  69. *p = av_malloc(min_size);
  70. if (!*p) min_size = 0;
  71. *size= min_size;
  72. }
  73. /* encoder management */
  74. static AVCodec *first_avcodec = NULL;
  75. AVCodec *av_codec_next(AVCodec *c){
  76. if(c) return c->next;
  77. else return first_avcodec;
  78. }
  79. #if !FF_API_AVCODEC_INIT
  80. static
  81. #endif
  82. void avcodec_init(void)
  83. {
  84. static int initialized = 0;
  85. if (initialized != 0)
  86. return;
  87. initialized = 1;
  88. dsputil_static_init();
  89. }
  90. static av_always_inline int codec_is_encoder(AVCodec *codec)
  91. {
  92. return codec && (codec->encode || codec->encode2);
  93. }
  94. static av_always_inline int codec_is_decoder(AVCodec *codec)
  95. {
  96. return codec && codec->decode;
  97. }
  98. void avcodec_register(AVCodec *codec)
  99. {
  100. AVCodec **p;
  101. avcodec_init();
  102. p = &first_avcodec;
  103. while (*p != NULL) p = &(*p)->next;
  104. *p = codec;
  105. codec->next = NULL;
  106. if (codec->init_static_data)
  107. codec->init_static_data(codec);
  108. }
  109. unsigned avcodec_get_edge_width(void)
  110. {
  111. return EDGE_WIDTH;
  112. }
  113. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  114. s->coded_width = width;
  115. s->coded_height= height;
  116. s->width = -((-width )>>s->lowres);
  117. s->height= -((-height)>>s->lowres);
  118. }
  119. #define INTERNAL_BUFFER_SIZE (32+1)
  120. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  121. int linesize_align[AV_NUM_DATA_POINTERS])
  122. {
  123. int i;
  124. int w_align= 1;
  125. int h_align= 1;
  126. switch(s->pix_fmt){
  127. case PIX_FMT_YUV420P:
  128. case PIX_FMT_YUYV422:
  129. case PIX_FMT_UYVY422:
  130. case PIX_FMT_YUV422P:
  131. case PIX_FMT_YUV440P:
  132. case PIX_FMT_YUV444P:
  133. case PIX_FMT_GBRP:
  134. case PIX_FMT_GRAY8:
  135. case PIX_FMT_GRAY16BE:
  136. case PIX_FMT_GRAY16LE:
  137. case PIX_FMT_YUVJ420P:
  138. case PIX_FMT_YUVJ422P:
  139. case PIX_FMT_YUVJ440P:
  140. case PIX_FMT_YUVJ444P:
  141. case PIX_FMT_YUVA420P:
  142. case PIX_FMT_YUV420P9LE:
  143. case PIX_FMT_YUV420P9BE:
  144. case PIX_FMT_YUV420P10LE:
  145. case PIX_FMT_YUV420P10BE:
  146. case PIX_FMT_YUV422P9LE:
  147. case PIX_FMT_YUV422P9BE:
  148. case PIX_FMT_YUV422P10LE:
  149. case PIX_FMT_YUV422P10BE:
  150. case PIX_FMT_YUV444P9LE:
  151. case PIX_FMT_YUV444P9BE:
  152. case PIX_FMT_YUV444P10LE:
  153. case PIX_FMT_YUV444P10BE:
  154. case PIX_FMT_GBRP9LE:
  155. case PIX_FMT_GBRP9BE:
  156. case PIX_FMT_GBRP10LE:
  157. case PIX_FMT_GBRP10BE:
  158. w_align = 16; //FIXME assume 16 pixel per macroblock
  159. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  160. break;
  161. case PIX_FMT_YUV411P:
  162. case PIX_FMT_UYYVYY411:
  163. w_align=32;
  164. h_align=8;
  165. break;
  166. case PIX_FMT_YUV410P:
  167. if(s->codec_id == CODEC_ID_SVQ1){
  168. w_align=64;
  169. h_align=64;
  170. }
  171. case PIX_FMT_RGB555:
  172. if(s->codec_id == CODEC_ID_RPZA){
  173. w_align=4;
  174. h_align=4;
  175. }
  176. case PIX_FMT_PAL8:
  177. case PIX_FMT_BGR8:
  178. case PIX_FMT_RGB8:
  179. if(s->codec_id == CODEC_ID_SMC){
  180. w_align=4;
  181. h_align=4;
  182. }
  183. break;
  184. case PIX_FMT_BGR24:
  185. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  186. w_align=4;
  187. h_align=4;
  188. }
  189. break;
  190. default:
  191. w_align= 1;
  192. h_align= 1;
  193. break;
  194. }
  195. *width = FFALIGN(*width , w_align);
  196. *height= FFALIGN(*height, h_align);
  197. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  198. *height+=2; // some of the optimized chroma MC reads one line too much
  199. // which is also done in mpeg decoders with lowres > 0
  200. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  201. linesize_align[i] = STRIDE_ALIGN;
  202. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  203. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  204. //picture size unneccessarily in some cases. The solution here is not
  205. //pretty and better ideas are welcome!
  206. #if HAVE_MMX
  207. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  208. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  209. s->codec_id == CODEC_ID_VP6A) {
  210. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  211. linesize_align[i] = 16;
  212. }
  213. #endif
  214. }
  215. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  216. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  217. int linesize_align[AV_NUM_DATA_POINTERS];
  218. int align;
  219. avcodec_align_dimensions2(s, width, height, linesize_align);
  220. align = FFMAX(linesize_align[0], linesize_align[3]);
  221. linesize_align[1] <<= chroma_shift;
  222. linesize_align[2] <<= chroma_shift;
  223. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  224. *width=FFALIGN(*width, align);
  225. }
  226. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  227. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  228. int buf_size, int align)
  229. {
  230. int ch, planar, needed_size, ret = 0;
  231. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  232. frame->nb_samples, sample_fmt,
  233. align);
  234. if (buf_size < needed_size)
  235. return AVERROR(EINVAL);
  236. planar = av_sample_fmt_is_planar(sample_fmt);
  237. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  238. if (!(frame->extended_data = av_mallocz(nb_channels *
  239. sizeof(*frame->extended_data))))
  240. return AVERROR(ENOMEM);
  241. } else {
  242. frame->extended_data = frame->data;
  243. }
  244. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  245. buf, nb_channels, frame->nb_samples,
  246. sample_fmt, align)) < 0) {
  247. if (frame->extended_data != frame->data)
  248. av_free(frame->extended_data);
  249. return ret;
  250. }
  251. if (frame->extended_data != frame->data) {
  252. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  253. frame->data[ch] = frame->extended_data[ch];
  254. }
  255. return ret;
  256. }
  257. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  258. {
  259. AVCodecInternal *avci = avctx->internal;
  260. InternalBuffer *buf;
  261. int buf_size, ret;
  262. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  263. frame->nb_samples, avctx->sample_fmt,
  264. 32);
  265. if (buf_size < 0)
  266. return AVERROR(EINVAL);
  267. /* allocate InternalBuffer if needed */
  268. if (!avci->buffer) {
  269. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  270. if (!avci->buffer)
  271. return AVERROR(ENOMEM);
  272. }
  273. buf = avci->buffer;
  274. /* if there is a previously-used internal buffer, check its size and
  275. channel count to see if we can reuse it */
  276. if (buf->extended_data) {
  277. /* if current buffer is too small, free it */
  278. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  279. av_free(buf->extended_data[0]);
  280. if (buf->extended_data != buf->data)
  281. av_free(&buf->extended_data);
  282. buf->extended_data = NULL;
  283. buf->data[0] = NULL;
  284. }
  285. /* if number of channels has changed, reset and/or free extended data
  286. pointers but leave data buffer in buf->data[0] for reuse */
  287. if (buf->nb_channels != avctx->channels) {
  288. if (buf->extended_data != buf->data)
  289. av_free(buf->extended_data);
  290. buf->extended_data = NULL;
  291. }
  292. }
  293. /* if there is no previous buffer or the previous buffer cannot be used
  294. as-is, allocate a new buffer and/or rearrange the channel pointers */
  295. if (!buf->extended_data) {
  296. if (!buf->data[0]) {
  297. if (!(buf->data[0] = av_mallocz(buf_size)))
  298. return AVERROR(ENOMEM);
  299. buf->audio_data_size = buf_size;
  300. }
  301. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  302. avctx->sample_fmt, buf->data[0],
  303. buf->audio_data_size, 32)))
  304. return ret;
  305. if (frame->extended_data == frame->data)
  306. buf->extended_data = buf->data;
  307. else
  308. buf->extended_data = frame->extended_data;
  309. memcpy(buf->data, frame->data, sizeof(frame->data));
  310. buf->linesize[0] = frame->linesize[0];
  311. buf->nb_channels = avctx->channels;
  312. } else {
  313. /* copy InternalBuffer info to the AVFrame */
  314. frame->extended_data = buf->extended_data;
  315. frame->linesize[0] = buf->linesize[0];
  316. memcpy(frame->data, buf->data, sizeof(frame->data));
  317. }
  318. frame->type = FF_BUFFER_TYPE_INTERNAL;
  319. if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
  320. else frame->pkt_pts = AV_NOPTS_VALUE;
  321. frame->reordered_opaque = avctx->reordered_opaque;
  322. if (avctx->debug & FF_DEBUG_BUFFERS)
  323. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  324. "internal audio buffer used\n", frame);
  325. return 0;
  326. }
  327. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  328. {
  329. int i;
  330. int w= s->width;
  331. int h= s->height;
  332. InternalBuffer *buf;
  333. AVCodecInternal *avci = s->internal;
  334. if(pic->data[0]!=NULL) {
  335. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  336. return -1;
  337. }
  338. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  339. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  340. return -1;
  341. }
  342. if(av_image_check_size(w, h, 0, s))
  343. return -1;
  344. if (!avci->buffer) {
  345. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  346. sizeof(InternalBuffer));
  347. }
  348. buf = &avci->buffer[avci->buffer_count];
  349. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  350. if(s->active_thread_type&FF_THREAD_FRAME) {
  351. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  352. return -1;
  353. }
  354. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  355. av_freep(&buf->base[i]);
  356. buf->data[i]= NULL;
  357. }
  358. }
  359. if (!buf->base[0]) {
  360. int h_chroma_shift, v_chroma_shift;
  361. int size[4] = {0};
  362. int tmpsize;
  363. int unaligned;
  364. AVPicture picture;
  365. int stride_align[AV_NUM_DATA_POINTERS];
  366. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  367. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  368. avcodec_align_dimensions2(s, &w, &h, stride_align);
  369. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  370. w+= EDGE_WIDTH*2;
  371. h+= EDGE_WIDTH*2;
  372. }
  373. do {
  374. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  375. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  376. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  377. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  378. w += w & ~(w-1);
  379. unaligned = 0;
  380. for (i=0; i<4; i++){
  381. unaligned |= picture.linesize[i] % stride_align[i];
  382. }
  383. } while (unaligned);
  384. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  385. if (tmpsize < 0)
  386. return -1;
  387. for (i=0; i<3 && picture.data[i+1]; i++)
  388. size[i] = picture.data[i+1] - picture.data[i];
  389. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  390. memset(buf->base, 0, sizeof(buf->base));
  391. memset(buf->data, 0, sizeof(buf->data));
  392. for(i=0; i<4 && size[i]; i++){
  393. const int h_shift= i==0 ? 0 : h_chroma_shift;
  394. const int v_shift= i==0 ? 0 : v_chroma_shift;
  395. buf->linesize[i]= picture.linesize[i];
  396. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  397. if(buf->base[i]==NULL) return -1;
  398. memset(buf->base[i], 128, size[i]);
  399. // no edge if EDGE EMU or not planar YUV
  400. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  401. buf->data[i] = buf->base[i];
  402. else
  403. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  404. }
  405. for (; i < AV_NUM_DATA_POINTERS; i++) {
  406. buf->base[i] = buf->data[i] = NULL;
  407. buf->linesize[i] = 0;
  408. }
  409. if(size[1] && !size[2])
  410. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  411. buf->width = s->width;
  412. buf->height = s->height;
  413. buf->pix_fmt= s->pix_fmt;
  414. }
  415. pic->type= FF_BUFFER_TYPE_INTERNAL;
  416. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  417. pic->base[i]= buf->base[i];
  418. pic->data[i]= buf->data[i];
  419. pic->linesize[i]= buf->linesize[i];
  420. }
  421. pic->extended_data = pic->data;
  422. avci->buffer_count++;
  423. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  424. else pic->pkt_pts= AV_NOPTS_VALUE;
  425. pic->reordered_opaque= s->reordered_opaque;
  426. if(s->debug&FF_DEBUG_BUFFERS)
  427. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  428. "buffers used\n", pic, avci->buffer_count);
  429. return 0;
  430. }
  431. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  432. {
  433. switch (avctx->codec_type) {
  434. case AVMEDIA_TYPE_VIDEO:
  435. return video_get_buffer(avctx, frame);
  436. case AVMEDIA_TYPE_AUDIO:
  437. return audio_get_buffer(avctx, frame);
  438. default:
  439. return -1;
  440. }
  441. }
  442. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  443. int i;
  444. InternalBuffer *buf, *last;
  445. AVCodecInternal *avci = s->internal;
  446. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  447. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  448. assert(avci->buffer_count);
  449. if (avci->buffer) {
  450. buf = NULL; /* avoids warning */
  451. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  452. buf = &avci->buffer[i];
  453. if (buf->data[0] == pic->data[0])
  454. break;
  455. }
  456. assert(i < avci->buffer_count);
  457. avci->buffer_count--;
  458. last = &avci->buffer[avci->buffer_count];
  459. if (buf != last)
  460. FFSWAP(InternalBuffer, *buf, *last);
  461. }
  462. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  463. pic->data[i]=NULL;
  464. // pic->base[i]=NULL;
  465. }
  466. //printf("R%X\n", pic->opaque);
  467. if(s->debug&FF_DEBUG_BUFFERS)
  468. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  469. "buffers used\n", pic, avci->buffer_count);
  470. }
  471. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  472. AVFrame temp_pic;
  473. int i;
  474. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  475. /* If no picture return a new buffer */
  476. if(pic->data[0] == NULL) {
  477. /* We will copy from buffer, so must be readable */
  478. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  479. return s->get_buffer(s, pic);
  480. }
  481. /* If internal buffer type return the same buffer */
  482. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  483. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  484. else pic->pkt_pts= AV_NOPTS_VALUE;
  485. pic->reordered_opaque= s->reordered_opaque;
  486. return 0;
  487. }
  488. /*
  489. * Not internal type and reget_buffer not overridden, emulate cr buffer
  490. */
  491. temp_pic = *pic;
  492. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  493. pic->data[i] = pic->base[i] = NULL;
  494. pic->opaque = NULL;
  495. /* Allocate new frame */
  496. if (s->get_buffer(s, pic))
  497. return -1;
  498. /* Copy image data from old buffer to new buffer */
  499. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  500. s->height);
  501. s->release_buffer(s, &temp_pic); // Release old frame
  502. return 0;
  503. }
  504. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  505. int i;
  506. for(i=0; i<count; i++){
  507. int r= func(c, (char*)arg + i*size);
  508. if(ret) ret[i]= r;
  509. }
  510. return 0;
  511. }
  512. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  513. int i;
  514. for(i=0; i<count; i++){
  515. int r= func(c, arg, i, 0);
  516. if(ret) ret[i]= r;
  517. }
  518. return 0;
  519. }
  520. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  521. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  522. ++fmt;
  523. return fmt[0];
  524. }
  525. void avcodec_get_frame_defaults(AVFrame *pic){
  526. memset(pic, 0, sizeof(AVFrame));
  527. pic->pts= AV_NOPTS_VALUE;
  528. pic->key_frame= 1;
  529. pic->sample_aspect_ratio = (AVRational){0, 1};
  530. pic->format = -1; /* unknown */
  531. }
  532. AVFrame *avcodec_alloc_frame(void){
  533. AVFrame *pic= av_malloc(sizeof(AVFrame));
  534. if(pic==NULL) return NULL;
  535. avcodec_get_frame_defaults(pic);
  536. return pic;
  537. }
  538. #if FF_API_AVCODEC_OPEN
  539. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  540. {
  541. return avcodec_open2(avctx, codec, NULL);
  542. }
  543. #endif
  544. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  545. {
  546. int ret = 0;
  547. AVDictionary *tmp = NULL;
  548. if (avcodec_is_open(avctx))
  549. return 0;
  550. if ((!codec && !avctx->codec)) {
  551. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  552. return AVERROR(EINVAL);
  553. }
  554. if ((codec && avctx->codec && codec != avctx->codec)) {
  555. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  556. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  557. return AVERROR(EINVAL);
  558. }
  559. if (!codec)
  560. codec = avctx->codec;
  561. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  562. return AVERROR(EINVAL);
  563. if (options)
  564. av_dict_copy(&tmp, *options, 0);
  565. /* If there is a user-supplied mutex locking routine, call it. */
  566. if (ff_lockmgr_cb) {
  567. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  568. return -1;
  569. }
  570. entangled_thread_counter++;
  571. if(entangled_thread_counter != 1){
  572. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  573. ret = -1;
  574. goto end;
  575. }
  576. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  577. if (!avctx->internal) {
  578. ret = AVERROR(ENOMEM);
  579. goto end;
  580. }
  581. if (codec->priv_data_size > 0) {
  582. if(!avctx->priv_data){
  583. avctx->priv_data = av_mallocz(codec->priv_data_size);
  584. if (!avctx->priv_data) {
  585. ret = AVERROR(ENOMEM);
  586. goto end;
  587. }
  588. if (codec->priv_class) {
  589. *(AVClass**)avctx->priv_data= codec->priv_class;
  590. av_opt_set_defaults(avctx->priv_data);
  591. }
  592. }
  593. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  594. goto free_and_end;
  595. } else {
  596. avctx->priv_data = NULL;
  597. }
  598. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  599. goto free_and_end;
  600. if(avctx->coded_width && avctx->coded_height)
  601. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  602. else if(avctx->width && avctx->height)
  603. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  604. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  605. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  606. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  607. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  608. avcodec_set_dimensions(avctx, 0, 0);
  609. }
  610. /* if the decoder init function was already called previously,
  611. free the already allocated subtitle_header before overwriting it */
  612. if (codec_is_decoder(codec))
  613. av_freep(&avctx->subtitle_header);
  614. #define SANE_NB_CHANNELS 128U
  615. if (avctx->channels > SANE_NB_CHANNELS) {
  616. ret = AVERROR(EINVAL);
  617. goto free_and_end;
  618. }
  619. avctx->codec = codec;
  620. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  621. avctx->codec_id == CODEC_ID_NONE) {
  622. avctx->codec_type = codec->type;
  623. avctx->codec_id = codec->id;
  624. }
  625. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  626. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  627. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  628. ret = AVERROR(EINVAL);
  629. goto free_and_end;
  630. }
  631. avctx->frame_number = 0;
  632. #if FF_API_ER
  633. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
  634. avctx->error_recognition, avctx->err_recognition);
  635. /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
  636. FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
  637. avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
  638. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
  639. avctx->error_recognition, avctx->err_recognition);
  640. #endif
  641. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  642. (!avctx->time_base.num || !avctx->time_base.den)) {
  643. avctx->time_base.num = 1;
  644. avctx->time_base.den = avctx->sample_rate;
  645. }
  646. if (HAVE_THREADS && !avctx->thread_opaque) {
  647. ret = ff_thread_init(avctx);
  648. if (ret < 0) {
  649. goto free_and_end;
  650. }
  651. }
  652. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  653. avctx->thread_count = 1;
  654. if (avctx->codec->max_lowres < avctx->lowres) {
  655. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  656. avctx->codec->max_lowres);
  657. ret = AVERROR(EINVAL);
  658. goto free_and_end;
  659. }
  660. if (codec_is_encoder(avctx->codec)) {
  661. int i;
  662. if (avctx->codec->sample_fmts) {
  663. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  664. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  665. break;
  666. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  667. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  668. ret = AVERROR(EINVAL);
  669. goto free_and_end;
  670. }
  671. }
  672. if (avctx->codec->supported_samplerates) {
  673. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  674. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  675. break;
  676. if (avctx->codec->supported_samplerates[i] == 0) {
  677. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  678. ret = AVERROR(EINVAL);
  679. goto free_and_end;
  680. }
  681. }
  682. if (avctx->codec->channel_layouts) {
  683. if (!avctx->channel_layout) {
  684. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  685. } else {
  686. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  687. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  688. break;
  689. if (avctx->codec->channel_layouts[i] == 0) {
  690. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  691. ret = AVERROR(EINVAL);
  692. goto free_and_end;
  693. }
  694. }
  695. }
  696. if (avctx->channel_layout && avctx->channels) {
  697. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  698. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  699. ret = AVERROR(EINVAL);
  700. goto free_and_end;
  701. }
  702. } else if (avctx->channel_layout) {
  703. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  704. }
  705. }
  706. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  707. ret = avctx->codec->init(avctx);
  708. if (ret < 0) {
  709. goto free_and_end;
  710. }
  711. }
  712. end:
  713. entangled_thread_counter--;
  714. /* Release any user-supplied mutex. */
  715. if (ff_lockmgr_cb) {
  716. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  717. }
  718. if (options) {
  719. av_dict_free(options);
  720. *options = tmp;
  721. }
  722. return ret;
  723. free_and_end:
  724. av_dict_free(&tmp);
  725. av_freep(&avctx->priv_data);
  726. av_freep(&avctx->internal);
  727. avctx->codec= NULL;
  728. goto end;
  729. }
  730. int ff_alloc_packet(AVPacket *avpkt, int size)
  731. {
  732. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  733. return AVERROR(EINVAL);
  734. if (avpkt->data) {
  735. uint8_t *pkt_data;
  736. int pkt_size;
  737. if (avpkt->size < size)
  738. return AVERROR(EINVAL);
  739. pkt_data = avpkt->data;
  740. pkt_size = avpkt->size;
  741. av_init_packet(avpkt);
  742. avpkt->data = pkt_data;
  743. avpkt->size = pkt_size;
  744. return 0;
  745. } else {
  746. return av_new_packet(avpkt, size);
  747. }
  748. }
  749. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  750. AVPacket *avpkt,
  751. const AVFrame *frame,
  752. int *got_packet_ptr)
  753. {
  754. int ret;
  755. int user_packet = !!avpkt->data;
  756. int nb_samples;
  757. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  758. av_init_packet(avpkt);
  759. avpkt->size = 0;
  760. return 0;
  761. }
  762. /* check for valid frame size */
  763. if (frame) {
  764. nb_samples = frame->nb_samples;
  765. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  766. if (nb_samples > avctx->frame_size)
  767. return AVERROR(EINVAL);
  768. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  769. if (nb_samples != avctx->frame_size)
  770. return AVERROR(EINVAL);
  771. }
  772. } else {
  773. nb_samples = avctx->frame_size;
  774. }
  775. if (avctx->codec->encode2) {
  776. *got_packet_ptr = 0;
  777. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  778. if (!ret && *got_packet_ptr &&
  779. !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  780. avpkt->pts = frame->pts;
  781. avpkt->duration = av_rescale_q(frame->nb_samples,
  782. (AVRational){ 1, avctx->sample_rate },
  783. avctx->time_base);
  784. }
  785. } else {
  786. /* for compatibility with encoders not supporting encode2(), we need to
  787. allocate a packet buffer if the user has not provided one or check
  788. the size otherwise */
  789. int fs_tmp = 0;
  790. int buf_size = avpkt->size;
  791. if (!user_packet) {
  792. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  793. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  794. buf_size = nb_samples * avctx->channels *
  795. av_get_bits_per_sample(avctx->codec_id) / 8;
  796. } else {
  797. /* this is a guess as to the required size.
  798. if an encoder needs more than this, it should probably
  799. implement encode2() */
  800. buf_size = 2 * avctx->frame_size * avctx->channels *
  801. av_get_bytes_per_sample(avctx->sample_fmt);
  802. buf_size += FF_MIN_BUFFER_SIZE;
  803. }
  804. }
  805. if ((ret = ff_alloc_packet(avpkt, buf_size)))
  806. return ret;
  807. /* Encoders using AVCodec.encode() that support
  808. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  809. the smaller size when encoding the last frame.
  810. This code can be removed once all encoders supporting
  811. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  812. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  813. nb_samples < avctx->frame_size) {
  814. fs_tmp = avctx->frame_size;
  815. avctx->frame_size = nb_samples;
  816. }
  817. /* encode the frame */
  818. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  819. frame ? frame->data[0] : NULL);
  820. if (ret >= 0) {
  821. if (!ret) {
  822. /* no output. if the packet data was allocated by libavcodec,
  823. free it */
  824. if (!user_packet)
  825. av_freep(&avpkt->data);
  826. } else {
  827. if (avctx->coded_frame)
  828. avpkt->pts = avctx->coded_frame->pts;
  829. /* Set duration for final small packet. This can be removed
  830. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  831. encode2() */
  832. if (fs_tmp) {
  833. avpkt->duration = av_rescale_q(avctx->frame_size,
  834. (AVRational){ 1, avctx->sample_rate },
  835. avctx->time_base);
  836. }
  837. }
  838. avpkt->size = ret;
  839. *got_packet_ptr = (ret > 0);
  840. ret = 0;
  841. }
  842. if (fs_tmp)
  843. avctx->frame_size = fs_tmp;
  844. }
  845. if (!ret)
  846. avctx->frame_number++;
  847. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  848. this needs to be moved to the encoders, but for now we can do it
  849. here to simplify things */
  850. avpkt->flags |= AV_PKT_FLAG_KEY;
  851. return ret;
  852. }
  853. #if FF_API_OLD_DECODE_AUDIO
  854. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  855. uint8_t *buf, int buf_size,
  856. const short *samples)
  857. {
  858. AVPacket pkt;
  859. AVFrame frame0;
  860. AVFrame *frame;
  861. int ret, samples_size, got_packet;
  862. av_init_packet(&pkt);
  863. pkt.data = buf;
  864. pkt.size = buf_size;
  865. if (samples) {
  866. frame = &frame0;
  867. avcodec_get_frame_defaults(frame);
  868. if (avctx->frame_size) {
  869. frame->nb_samples = avctx->frame_size;
  870. } else {
  871. /* if frame_size is not set, the number of samples must be
  872. calculated from the buffer size */
  873. int64_t nb_samples;
  874. if (!av_get_bits_per_sample(avctx->codec_id)) {
  875. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  876. "support this codec\n");
  877. return AVERROR(EINVAL);
  878. }
  879. nb_samples = (int64_t)buf_size * 8 /
  880. (av_get_bits_per_sample(avctx->codec_id) *
  881. avctx->channels);
  882. if (nb_samples >= INT_MAX)
  883. return AVERROR(EINVAL);
  884. frame->nb_samples = nb_samples;
  885. }
  886. /* it is assumed that the samples buffer is large enough based on the
  887. relevant parameters */
  888. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  889. frame->nb_samples,
  890. avctx->sample_fmt, 1);
  891. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  892. avctx->sample_fmt,
  893. samples, samples_size, 1)))
  894. return ret;
  895. /* fabricate frame pts from sample count.
  896. this is needed because the avcodec_encode_audio() API does not have
  897. a way for the user to provide pts */
  898. frame->pts = av_rescale_q(avctx->internal->sample_count,
  899. (AVRational){ 1, avctx->sample_rate },
  900. avctx->time_base);
  901. avctx->internal->sample_count += frame->nb_samples;
  902. } else {
  903. frame = NULL;
  904. }
  905. got_packet = 0;
  906. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  907. if (!ret && got_packet && avctx->coded_frame) {
  908. avctx->coded_frame->pts = pkt.pts;
  909. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  910. }
  911. /* free any side data since we cannot return it */
  912. if (pkt.side_data_elems > 0) {
  913. int i;
  914. for (i = 0; i < pkt.side_data_elems; i++)
  915. av_free(pkt.side_data[i].data);
  916. av_freep(&pkt.side_data);
  917. pkt.side_data_elems = 0;
  918. }
  919. if (frame && frame->extended_data != frame->data)
  920. av_free(frame->extended_data);
  921. return ret ? ret : pkt.size;
  922. }
  923. #endif
  924. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  925. const AVFrame *pict)
  926. {
  927. if(buf_size < FF_MIN_BUFFER_SIZE){
  928. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  929. return -1;
  930. }
  931. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  932. return -1;
  933. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  934. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  935. avctx->frame_number++;
  936. emms_c(); //needed to avoid an emms_c() call before every return;
  937. return ret;
  938. }else
  939. return 0;
  940. }
  941. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  942. const AVSubtitle *sub)
  943. {
  944. int ret;
  945. if(sub->start_display_time) {
  946. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  947. return -1;
  948. }
  949. if(sub->num_rects == 0 || !sub->rects)
  950. return -1;
  951. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  952. avctx->frame_number++;
  953. return ret;
  954. }
  955. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  956. {
  957. int size = 0;
  958. const uint8_t *data;
  959. uint32_t flags;
  960. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  961. return;
  962. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  963. if (!data || size < 4)
  964. return;
  965. flags = bytestream_get_le32(&data);
  966. size -= 4;
  967. if (size < 4) /* Required for any of the changes */
  968. return;
  969. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  970. avctx->channels = bytestream_get_le32(&data);
  971. size -= 4;
  972. }
  973. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  974. if (size < 8)
  975. return;
  976. avctx->channel_layout = bytestream_get_le64(&data);
  977. size -= 8;
  978. }
  979. if (size < 4)
  980. return;
  981. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  982. avctx->sample_rate = bytestream_get_le32(&data);
  983. size -= 4;
  984. }
  985. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  986. if (size < 8)
  987. return;
  988. avctx->width = bytestream_get_le32(&data);
  989. avctx->height = bytestream_get_le32(&data);
  990. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  991. size -= 8;
  992. }
  993. }
  994. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  995. int *got_picture_ptr,
  996. AVPacket *avpkt)
  997. {
  998. int ret;
  999. *got_picture_ptr= 0;
  1000. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1001. return -1;
  1002. avctx->pkt = avpkt;
  1003. apply_param_change(avctx, avpkt);
  1004. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1005. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1006. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1007. avpkt);
  1008. else {
  1009. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1010. avpkt);
  1011. picture->pkt_dts= avpkt->dts;
  1012. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1013. picture->width = avctx->width;
  1014. picture->height = avctx->height;
  1015. picture->format = avctx->pix_fmt;
  1016. }
  1017. emms_c(); //needed to avoid an emms_c() call before every return;
  1018. if (*got_picture_ptr)
  1019. avctx->frame_number++;
  1020. }else
  1021. ret= 0;
  1022. return ret;
  1023. }
  1024. #if FF_API_OLD_DECODE_AUDIO
  1025. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1026. int *frame_size_ptr,
  1027. AVPacket *avpkt)
  1028. {
  1029. AVFrame frame;
  1030. int ret, got_frame = 0;
  1031. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1032. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1033. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1034. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1035. "avcodec_decode_audio4()\n");
  1036. avctx->get_buffer = avcodec_default_get_buffer;
  1037. }
  1038. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1039. if (ret >= 0 && got_frame) {
  1040. int ch, plane_size;
  1041. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1042. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1043. frame.nb_samples,
  1044. avctx->sample_fmt, 1);
  1045. if (*frame_size_ptr < data_size) {
  1046. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1047. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1048. return AVERROR(EINVAL);
  1049. }
  1050. memcpy(samples, frame.extended_data[0], plane_size);
  1051. if (planar && avctx->channels > 1) {
  1052. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1053. for (ch = 1; ch < avctx->channels; ch++) {
  1054. memcpy(out, frame.extended_data[ch], plane_size);
  1055. out += plane_size;
  1056. }
  1057. }
  1058. *frame_size_ptr = data_size;
  1059. } else {
  1060. *frame_size_ptr = 0;
  1061. }
  1062. return ret;
  1063. }
  1064. #endif
  1065. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1066. AVFrame *frame,
  1067. int *got_frame_ptr,
  1068. AVPacket *avpkt)
  1069. {
  1070. int ret = 0;
  1071. *got_frame_ptr = 0;
  1072. avctx->pkt = avpkt;
  1073. if (!avpkt->data && avpkt->size) {
  1074. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1075. return AVERROR(EINVAL);
  1076. }
  1077. apply_param_change(avctx, avpkt);
  1078. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1079. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1080. if (ret >= 0 && *got_frame_ptr) {
  1081. avctx->frame_number++;
  1082. frame->pkt_dts = avpkt->dts;
  1083. if (frame->format == AV_SAMPLE_FMT_NONE)
  1084. frame->format = avctx->sample_fmt;
  1085. }
  1086. }
  1087. return ret;
  1088. }
  1089. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1090. int *got_sub_ptr,
  1091. AVPacket *avpkt)
  1092. {
  1093. int ret;
  1094. avctx->pkt = avpkt;
  1095. *got_sub_ptr = 0;
  1096. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1097. if (*got_sub_ptr)
  1098. avctx->frame_number++;
  1099. return ret;
  1100. }
  1101. void avsubtitle_free(AVSubtitle *sub)
  1102. {
  1103. int i;
  1104. for (i = 0; i < sub->num_rects; i++)
  1105. {
  1106. av_freep(&sub->rects[i]->pict.data[0]);
  1107. av_freep(&sub->rects[i]->pict.data[1]);
  1108. av_freep(&sub->rects[i]->pict.data[2]);
  1109. av_freep(&sub->rects[i]->pict.data[3]);
  1110. av_freep(&sub->rects[i]->text);
  1111. av_freep(&sub->rects[i]->ass);
  1112. av_freep(&sub->rects[i]);
  1113. }
  1114. av_freep(&sub->rects);
  1115. memset(sub, 0, sizeof(AVSubtitle));
  1116. }
  1117. av_cold int avcodec_close(AVCodecContext *avctx)
  1118. {
  1119. /* If there is a user-supplied mutex locking routine, call it. */
  1120. if (ff_lockmgr_cb) {
  1121. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1122. return -1;
  1123. }
  1124. entangled_thread_counter++;
  1125. if(entangled_thread_counter != 1){
  1126. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1127. entangled_thread_counter--;
  1128. return -1;
  1129. }
  1130. if (avcodec_is_open(avctx)) {
  1131. if (HAVE_THREADS && avctx->thread_opaque)
  1132. ff_thread_free(avctx);
  1133. if (avctx->codec && avctx->codec->close)
  1134. avctx->codec->close(avctx);
  1135. avcodec_default_free_buffers(avctx);
  1136. avctx->coded_frame = NULL;
  1137. av_freep(&avctx->internal);
  1138. }
  1139. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1140. av_opt_free(avctx->priv_data);
  1141. av_opt_free(avctx);
  1142. av_freep(&avctx->priv_data);
  1143. if (codec_is_encoder(avctx->codec))
  1144. av_freep(&avctx->extradata);
  1145. avctx->codec = NULL;
  1146. avctx->active_thread_type = 0;
  1147. entangled_thread_counter--;
  1148. /* Release any user-supplied mutex. */
  1149. if (ff_lockmgr_cb) {
  1150. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1151. }
  1152. return 0;
  1153. }
  1154. AVCodec *avcodec_find_encoder(enum CodecID id)
  1155. {
  1156. AVCodec *p, *experimental=NULL;
  1157. p = first_avcodec;
  1158. while (p) {
  1159. if (codec_is_encoder(p) && p->id == id) {
  1160. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1161. experimental = p;
  1162. } else
  1163. return p;
  1164. }
  1165. p = p->next;
  1166. }
  1167. return experimental;
  1168. }
  1169. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1170. {
  1171. AVCodec *p;
  1172. if (!name)
  1173. return NULL;
  1174. p = first_avcodec;
  1175. while (p) {
  1176. if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1177. return p;
  1178. p = p->next;
  1179. }
  1180. return NULL;
  1181. }
  1182. AVCodec *avcodec_find_decoder(enum CodecID id)
  1183. {
  1184. AVCodec *p;
  1185. p = first_avcodec;
  1186. while (p) {
  1187. if (codec_is_decoder(p) && p->id == id)
  1188. return p;
  1189. p = p->next;
  1190. }
  1191. return NULL;
  1192. }
  1193. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1194. {
  1195. AVCodec *p;
  1196. if (!name)
  1197. return NULL;
  1198. p = first_avcodec;
  1199. while (p) {
  1200. if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1201. return p;
  1202. p = p->next;
  1203. }
  1204. return NULL;
  1205. }
  1206. static int get_bit_rate(AVCodecContext *ctx)
  1207. {
  1208. int bit_rate;
  1209. int bits_per_sample;
  1210. switch(ctx->codec_type) {
  1211. case AVMEDIA_TYPE_VIDEO:
  1212. case AVMEDIA_TYPE_DATA:
  1213. case AVMEDIA_TYPE_SUBTITLE:
  1214. case AVMEDIA_TYPE_ATTACHMENT:
  1215. bit_rate = ctx->bit_rate;
  1216. break;
  1217. case AVMEDIA_TYPE_AUDIO:
  1218. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1219. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1220. break;
  1221. default:
  1222. bit_rate = 0;
  1223. break;
  1224. }
  1225. return bit_rate;
  1226. }
  1227. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1228. {
  1229. int i, len, ret = 0;
  1230. for (i = 0; i < 4; i++) {
  1231. len = snprintf(buf, buf_size,
  1232. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1233. buf += len;
  1234. buf_size = buf_size > len ? buf_size - len : 0;
  1235. ret += len;
  1236. codec_tag>>=8;
  1237. }
  1238. return ret;
  1239. }
  1240. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1241. {
  1242. const char *codec_name;
  1243. const char *profile = NULL;
  1244. AVCodec *p;
  1245. char buf1[32];
  1246. int bitrate;
  1247. AVRational display_aspect_ratio;
  1248. if (encode)
  1249. p = avcodec_find_encoder(enc->codec_id);
  1250. else
  1251. p = avcodec_find_decoder(enc->codec_id);
  1252. if (p) {
  1253. codec_name = p->name;
  1254. profile = av_get_profile_name(p, enc->profile);
  1255. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  1256. /* fake mpeg2 transport stream codec (currently not
  1257. registered) */
  1258. codec_name = "mpeg2ts";
  1259. } else if (enc->codec_name[0] != '\0') {
  1260. codec_name = enc->codec_name;
  1261. } else {
  1262. /* output avi tags */
  1263. char tag_buf[32];
  1264. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1265. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1266. codec_name = buf1;
  1267. }
  1268. switch(enc->codec_type) {
  1269. case AVMEDIA_TYPE_VIDEO:
  1270. snprintf(buf, buf_size,
  1271. "Video: %s%s",
  1272. codec_name, enc->mb_decision ? " (hq)" : "");
  1273. if (profile)
  1274. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1275. " (%s)", profile);
  1276. if (enc->pix_fmt != PIX_FMT_NONE) {
  1277. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1278. ", %s",
  1279. av_get_pix_fmt_name(enc->pix_fmt));
  1280. }
  1281. if (enc->width) {
  1282. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1283. ", %dx%d",
  1284. enc->width, enc->height);
  1285. if (enc->sample_aspect_ratio.num) {
  1286. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1287. enc->width*enc->sample_aspect_ratio.num,
  1288. enc->height*enc->sample_aspect_ratio.den,
  1289. 1024*1024);
  1290. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1291. " [PAR %d:%d DAR %d:%d]",
  1292. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1293. display_aspect_ratio.num, display_aspect_ratio.den);
  1294. }
  1295. if(av_log_get_level() >= AV_LOG_DEBUG){
  1296. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1297. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1298. ", %d/%d",
  1299. enc->time_base.num/g, enc->time_base.den/g);
  1300. }
  1301. }
  1302. if (encode) {
  1303. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1304. ", q=%d-%d", enc->qmin, enc->qmax);
  1305. }
  1306. break;
  1307. case AVMEDIA_TYPE_AUDIO:
  1308. snprintf(buf, buf_size,
  1309. "Audio: %s",
  1310. codec_name);
  1311. if (profile)
  1312. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1313. " (%s)", profile);
  1314. if (enc->sample_rate) {
  1315. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1316. ", %d Hz", enc->sample_rate);
  1317. }
  1318. av_strlcat(buf, ", ", buf_size);
  1319. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1320. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1321. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1322. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1323. }
  1324. break;
  1325. case AVMEDIA_TYPE_DATA:
  1326. snprintf(buf, buf_size, "Data: %s", codec_name);
  1327. break;
  1328. case AVMEDIA_TYPE_SUBTITLE:
  1329. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1330. break;
  1331. case AVMEDIA_TYPE_ATTACHMENT:
  1332. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1333. break;
  1334. default:
  1335. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1336. return;
  1337. }
  1338. if (encode) {
  1339. if (enc->flags & CODEC_FLAG_PASS1)
  1340. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1341. ", pass 1");
  1342. if (enc->flags & CODEC_FLAG_PASS2)
  1343. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1344. ", pass 2");
  1345. }
  1346. bitrate = get_bit_rate(enc);
  1347. if (bitrate != 0) {
  1348. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1349. ", %d kb/s", bitrate / 1000);
  1350. }
  1351. }
  1352. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1353. {
  1354. const AVProfile *p;
  1355. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1356. return NULL;
  1357. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1358. if (p->profile == profile)
  1359. return p->name;
  1360. return NULL;
  1361. }
  1362. unsigned avcodec_version( void )
  1363. {
  1364. return LIBAVCODEC_VERSION_INT;
  1365. }
  1366. const char *avcodec_configuration(void)
  1367. {
  1368. return LIBAV_CONFIGURATION;
  1369. }
  1370. const char *avcodec_license(void)
  1371. {
  1372. #define LICENSE_PREFIX "libavcodec license: "
  1373. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1374. }
  1375. void avcodec_flush_buffers(AVCodecContext *avctx)
  1376. {
  1377. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1378. ff_thread_flush(avctx);
  1379. else if(avctx->codec->flush)
  1380. avctx->codec->flush(avctx);
  1381. }
  1382. static void video_free_buffers(AVCodecContext *s)
  1383. {
  1384. AVCodecInternal *avci = s->internal;
  1385. int i, j;
  1386. if (!avci->buffer)
  1387. return;
  1388. if (avci->buffer_count)
  1389. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1390. avci->buffer_count);
  1391. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1392. InternalBuffer *buf = &avci->buffer[i];
  1393. for(j=0; j<4; j++){
  1394. av_freep(&buf->base[j]);
  1395. buf->data[j]= NULL;
  1396. }
  1397. }
  1398. av_freep(&avci->buffer);
  1399. avci->buffer_count=0;
  1400. }
  1401. static void audio_free_buffers(AVCodecContext *avctx)
  1402. {
  1403. AVCodecInternal *avci = avctx->internal;
  1404. InternalBuffer *buf;
  1405. if (!avci->buffer)
  1406. return;
  1407. buf = avci->buffer;
  1408. if (buf->extended_data) {
  1409. av_free(buf->extended_data[0]);
  1410. if (buf->extended_data != buf->data)
  1411. av_free(buf->extended_data);
  1412. }
  1413. av_freep(&avci->buffer);
  1414. }
  1415. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1416. {
  1417. switch (avctx->codec_type) {
  1418. case AVMEDIA_TYPE_VIDEO:
  1419. video_free_buffers(avctx);
  1420. break;
  1421. case AVMEDIA_TYPE_AUDIO:
  1422. audio_free_buffers(avctx);
  1423. break;
  1424. default:
  1425. break;
  1426. }
  1427. }
  1428. #if FF_API_OLD_FF_PICT_TYPES
  1429. char av_get_pict_type_char(int pict_type){
  1430. return av_get_picture_type_char(pict_type);
  1431. }
  1432. #endif
  1433. int av_get_bits_per_sample(enum CodecID codec_id){
  1434. switch(codec_id){
  1435. case CODEC_ID_ADPCM_SBPRO_2:
  1436. return 2;
  1437. case CODEC_ID_ADPCM_SBPRO_3:
  1438. return 3;
  1439. case CODEC_ID_ADPCM_SBPRO_4:
  1440. case CODEC_ID_ADPCM_CT:
  1441. case CODEC_ID_ADPCM_IMA_WAV:
  1442. case CODEC_ID_ADPCM_IMA_QT:
  1443. case CODEC_ID_ADPCM_SWF:
  1444. case CODEC_ID_ADPCM_MS:
  1445. case CODEC_ID_ADPCM_YAMAHA:
  1446. case CODEC_ID_ADPCM_G722:
  1447. return 4;
  1448. case CODEC_ID_PCM_ALAW:
  1449. case CODEC_ID_PCM_MULAW:
  1450. case CODEC_ID_PCM_S8:
  1451. case CODEC_ID_PCM_U8:
  1452. case CODEC_ID_PCM_ZORK:
  1453. return 8;
  1454. case CODEC_ID_PCM_S16BE:
  1455. case CODEC_ID_PCM_S16LE:
  1456. case CODEC_ID_PCM_S16LE_PLANAR:
  1457. case CODEC_ID_PCM_U16BE:
  1458. case CODEC_ID_PCM_U16LE:
  1459. return 16;
  1460. case CODEC_ID_PCM_S24DAUD:
  1461. case CODEC_ID_PCM_S24BE:
  1462. case CODEC_ID_PCM_S24LE:
  1463. case CODEC_ID_PCM_U24BE:
  1464. case CODEC_ID_PCM_U24LE:
  1465. return 24;
  1466. case CODEC_ID_PCM_S32BE:
  1467. case CODEC_ID_PCM_S32LE:
  1468. case CODEC_ID_PCM_U32BE:
  1469. case CODEC_ID_PCM_U32LE:
  1470. case CODEC_ID_PCM_F32BE:
  1471. case CODEC_ID_PCM_F32LE:
  1472. return 32;
  1473. case CODEC_ID_PCM_F64BE:
  1474. case CODEC_ID_PCM_F64LE:
  1475. return 64;
  1476. default:
  1477. return 0;
  1478. }
  1479. }
  1480. #if FF_API_OLD_SAMPLE_FMT
  1481. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1482. return av_get_bytes_per_sample(sample_fmt) << 3;
  1483. }
  1484. #endif
  1485. #if !HAVE_THREADS
  1486. int ff_thread_init(AVCodecContext *s){
  1487. return -1;
  1488. }
  1489. #endif
  1490. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1491. {
  1492. unsigned int n = 0;
  1493. while(v >= 0xff) {
  1494. *s++ = 0xff;
  1495. v -= 0xff;
  1496. n++;
  1497. }
  1498. *s = v;
  1499. n++;
  1500. return n;
  1501. }
  1502. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1503. int i;
  1504. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1505. return i;
  1506. }
  1507. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1508. {
  1509. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1510. "version to the newest one from Git. If the problem still "
  1511. "occurs, it means that your file has a feature which has not "
  1512. "been implemented.\n", feature);
  1513. if(want_sample)
  1514. av_log_ask_for_sample(avc, NULL);
  1515. }
  1516. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1517. {
  1518. va_list argument_list;
  1519. va_start(argument_list, msg);
  1520. if (msg)
  1521. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1522. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1523. "of this file to ftp://upload.libav.org/incoming/ "
  1524. "and contact the libav-devel mailing list.\n");
  1525. va_end(argument_list);
  1526. }
  1527. static AVHWAccel *first_hwaccel = NULL;
  1528. void av_register_hwaccel(AVHWAccel *hwaccel)
  1529. {
  1530. AVHWAccel **p = &first_hwaccel;
  1531. while (*p)
  1532. p = &(*p)->next;
  1533. *p = hwaccel;
  1534. hwaccel->next = NULL;
  1535. }
  1536. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1537. {
  1538. return hwaccel ? hwaccel->next : first_hwaccel;
  1539. }
  1540. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1541. {
  1542. AVHWAccel *hwaccel=NULL;
  1543. while((hwaccel= av_hwaccel_next(hwaccel))){
  1544. if ( hwaccel->id == codec_id
  1545. && hwaccel->pix_fmt == pix_fmt)
  1546. return hwaccel;
  1547. }
  1548. return NULL;
  1549. }
  1550. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1551. {
  1552. if (ff_lockmgr_cb) {
  1553. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1554. return -1;
  1555. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1556. return -1;
  1557. }
  1558. ff_lockmgr_cb = cb;
  1559. if (ff_lockmgr_cb) {
  1560. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1561. return -1;
  1562. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1563. return -1;
  1564. }
  1565. return 0;
  1566. }
  1567. int avpriv_lock_avformat(void)
  1568. {
  1569. if (ff_lockmgr_cb) {
  1570. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1571. return -1;
  1572. }
  1573. return 0;
  1574. }
  1575. int avpriv_unlock_avformat(void)
  1576. {
  1577. if (ff_lockmgr_cb) {
  1578. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1579. return -1;
  1580. }
  1581. return 0;
  1582. }
  1583. unsigned int avpriv_toupper4(unsigned int x)
  1584. {
  1585. return toupper( x &0xFF)
  1586. + (toupper((x>>8 )&0xFF)<<8 )
  1587. + (toupper((x>>16)&0xFF)<<16)
  1588. + (toupper((x>>24)&0xFF)<<24);
  1589. }
  1590. #if !HAVE_THREADS
  1591. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1592. {
  1593. f->owner = avctx;
  1594. return avctx->get_buffer(avctx, f);
  1595. }
  1596. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1597. {
  1598. f->owner->release_buffer(f->owner, f);
  1599. }
  1600. void ff_thread_finish_setup(AVCodecContext *avctx)
  1601. {
  1602. }
  1603. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1604. {
  1605. }
  1606. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1607. {
  1608. }
  1609. #endif
  1610. #if FF_API_THREAD_INIT
  1611. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1612. {
  1613. s->thread_count = thread_count;
  1614. return ff_thread_init(s);
  1615. }
  1616. #endif
  1617. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1618. {
  1619. if (codec_id <= CODEC_ID_NONE)
  1620. return AVMEDIA_TYPE_UNKNOWN;
  1621. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1622. return AVMEDIA_TYPE_VIDEO;
  1623. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1624. return AVMEDIA_TYPE_AUDIO;
  1625. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1626. return AVMEDIA_TYPE_SUBTITLE;
  1627. return AVMEDIA_TYPE_UNKNOWN;
  1628. }
  1629. int avcodec_is_open(AVCodecContext *s)
  1630. {
  1631. return !!s->internal;
  1632. }