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.

2454 lines
76KB

  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/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 "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "imgconvert.h"
  40. #include "thread.h"
  41. #include "frame_thread_encoder.h"
  42. #include "audioconvert.h"
  43. #include "internal.h"
  44. #include "bytestream.h"
  45. #include <stdlib.h>
  46. #include <stdarg.h>
  47. #include <limits.h>
  48. #include <float.h>
  49. static int volatile entangled_thread_counter=0;
  50. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  51. static void *codec_mutex;
  52. static void *avformat_mutex;
  53. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  54. {
  55. if(min_size < *size)
  56. return ptr;
  57. min_size= FFMAX(17*min_size/16 + 32, min_size);
  58. ptr= av_realloc(ptr, min_size);
  59. 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
  60. min_size= 0;
  61. *size= min_size;
  62. return ptr;
  63. }
  64. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  65. {
  66. void **p = ptr;
  67. if (min_size < *size)
  68. return 0;
  69. min_size= FFMAX(17*min_size/16 + 32, min_size);
  70. av_free(*p);
  71. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  72. if (!*p) min_size = 0;
  73. *size= min_size;
  74. return 1;
  75. }
  76. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  77. {
  78. ff_fast_malloc(ptr, size, min_size, 0);
  79. }
  80. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  81. {
  82. uint8_t **p = ptr;
  83. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  84. av_freep(p);
  85. *size = 0;
  86. return;
  87. }
  88. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  89. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  90. }
  91. /* encoder management */
  92. static AVCodec *first_avcodec = NULL;
  93. AVCodec *av_codec_next(AVCodec *c){
  94. if(c) return c->next;
  95. else return first_avcodec;
  96. }
  97. static void avcodec_init(void)
  98. {
  99. static int initialized = 0;
  100. if (initialized != 0)
  101. return;
  102. initialized = 1;
  103. ff_dsputil_static_init();
  104. }
  105. int av_codec_is_encoder(AVCodec *codec)
  106. {
  107. return codec && (codec->encode || codec->encode2);
  108. }
  109. int av_codec_is_decoder(AVCodec *codec)
  110. {
  111. return codec && codec->decode;
  112. }
  113. void avcodec_register(AVCodec *codec)
  114. {
  115. AVCodec **p;
  116. avcodec_init();
  117. p = &first_avcodec;
  118. while (*p != NULL) p = &(*p)->next;
  119. *p = codec;
  120. codec->next = NULL;
  121. if (codec->init_static_data)
  122. codec->init_static_data(codec);
  123. }
  124. unsigned avcodec_get_edge_width(void)
  125. {
  126. return EDGE_WIDTH;
  127. }
  128. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  129. s->coded_width = width;
  130. s->coded_height= height;
  131. s->width = -((-width )>>s->lowres);
  132. s->height= -((-height)>>s->lowres);
  133. }
  134. #define INTERNAL_BUFFER_SIZE (32+1)
  135. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  136. int linesize_align[AV_NUM_DATA_POINTERS])
  137. {
  138. int i;
  139. int w_align= 1;
  140. int h_align= 1;
  141. switch(s->pix_fmt){
  142. case PIX_FMT_YUV420P:
  143. case PIX_FMT_YUYV422:
  144. case PIX_FMT_UYVY422:
  145. case PIX_FMT_YUV422P:
  146. case PIX_FMT_YUV440P:
  147. case PIX_FMT_YUV444P:
  148. case PIX_FMT_GBRP:
  149. case PIX_FMT_GRAY8:
  150. case PIX_FMT_GRAY16BE:
  151. case PIX_FMT_GRAY16LE:
  152. case PIX_FMT_YUVJ420P:
  153. case PIX_FMT_YUVJ422P:
  154. case PIX_FMT_YUVJ440P:
  155. case PIX_FMT_YUVJ444P:
  156. case PIX_FMT_YUVA420P:
  157. case PIX_FMT_YUVA422P:
  158. case PIX_FMT_YUVA444P:
  159. case PIX_FMT_YUV420P9LE:
  160. case PIX_FMT_YUV420P9BE:
  161. case PIX_FMT_YUV420P10LE:
  162. case PIX_FMT_YUV420P10BE:
  163. case PIX_FMT_YUV420P12LE:
  164. case PIX_FMT_YUV420P12BE:
  165. case PIX_FMT_YUV420P14LE:
  166. case PIX_FMT_YUV420P14BE:
  167. case PIX_FMT_YUV422P9LE:
  168. case PIX_FMT_YUV422P9BE:
  169. case PIX_FMT_YUV422P10LE:
  170. case PIX_FMT_YUV422P10BE:
  171. case PIX_FMT_YUV422P12LE:
  172. case PIX_FMT_YUV422P12BE:
  173. case PIX_FMT_YUV422P14LE:
  174. case PIX_FMT_YUV422P14BE:
  175. case PIX_FMT_YUV444P9LE:
  176. case PIX_FMT_YUV444P9BE:
  177. case PIX_FMT_YUV444P10LE:
  178. case PIX_FMT_YUV444P10BE:
  179. case PIX_FMT_YUV444P12LE:
  180. case PIX_FMT_YUV444P12BE:
  181. case PIX_FMT_YUV444P14LE:
  182. case PIX_FMT_YUV444P14BE:
  183. case PIX_FMT_GBRP9LE:
  184. case PIX_FMT_GBRP9BE:
  185. case PIX_FMT_GBRP10LE:
  186. case PIX_FMT_GBRP10BE:
  187. case PIX_FMT_GBRP12LE:
  188. case PIX_FMT_GBRP12BE:
  189. case PIX_FMT_GBRP14LE:
  190. case PIX_FMT_GBRP14BE:
  191. w_align = 16; //FIXME assume 16 pixel per macroblock
  192. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  193. break;
  194. case PIX_FMT_YUV411P:
  195. case PIX_FMT_UYYVYY411:
  196. w_align=32;
  197. h_align=8;
  198. break;
  199. case PIX_FMT_YUV410P:
  200. if(s->codec_id == CODEC_ID_SVQ1){
  201. w_align=64;
  202. h_align=64;
  203. }
  204. case PIX_FMT_RGB555:
  205. if(s->codec_id == CODEC_ID_RPZA){
  206. w_align=4;
  207. h_align=4;
  208. }
  209. case PIX_FMT_PAL8:
  210. case PIX_FMT_BGR8:
  211. case PIX_FMT_RGB8:
  212. if(s->codec_id == CODEC_ID_SMC){
  213. w_align=4;
  214. h_align=4;
  215. }
  216. break;
  217. case PIX_FMT_BGR24:
  218. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  219. w_align=4;
  220. h_align=4;
  221. }
  222. break;
  223. default:
  224. w_align= 1;
  225. h_align= 1;
  226. break;
  227. }
  228. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  229. w_align= FFMAX(w_align, 8);
  230. }
  231. *width = FFALIGN(*width , w_align);
  232. *height= FFALIGN(*height, h_align);
  233. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  234. *height+=2; // some of the optimized chroma MC reads one line too much
  235. // which is also done in mpeg decoders with lowres > 0
  236. for (i = 0; i < 4; i++)
  237. linesize_align[i] = STRIDE_ALIGN;
  238. }
  239. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  240. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  241. int linesize_align[AV_NUM_DATA_POINTERS];
  242. int align;
  243. avcodec_align_dimensions2(s, width, height, linesize_align);
  244. align = FFMAX(linesize_align[0], linesize_align[3]);
  245. linesize_align[1] <<= chroma_shift;
  246. linesize_align[2] <<= chroma_shift;
  247. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  248. *width=FFALIGN(*width, align);
  249. }
  250. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  251. {
  252. if (s->pkt) {
  253. pic->pkt_pts = s->pkt->pts;
  254. pic->pkt_pos = s->pkt->pos;
  255. pic->pkt_duration = s->pkt->duration;
  256. } else {
  257. pic->pkt_pts = AV_NOPTS_VALUE;
  258. pic->pkt_pos = -1;
  259. pic->pkt_duration = 0;
  260. }
  261. pic->reordered_opaque= s->reordered_opaque;
  262. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  263. pic->width = s->width;
  264. pic->height = s->height;
  265. pic->format = s->pix_fmt;
  266. }
  267. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  268. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  269. int buf_size, int align)
  270. {
  271. int ch, planar, needed_size, ret = 0;
  272. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  273. frame->nb_samples, sample_fmt,
  274. align);
  275. if (buf_size < needed_size)
  276. return AVERROR(EINVAL);
  277. planar = av_sample_fmt_is_planar(sample_fmt);
  278. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  279. if (!(frame->extended_data = av_mallocz(nb_channels *
  280. sizeof(*frame->extended_data))))
  281. return AVERROR(ENOMEM);
  282. } else {
  283. frame->extended_data = frame->data;
  284. }
  285. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  286. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  287. sample_fmt, align)) < 0) {
  288. if (frame->extended_data != frame->data)
  289. av_freep(&frame->extended_data);
  290. return ret;
  291. }
  292. if (frame->extended_data != frame->data) {
  293. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  294. frame->data[ch] = frame->extended_data[ch];
  295. }
  296. return ret;
  297. }
  298. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  299. {
  300. AVCodecInternal *avci = avctx->internal;
  301. InternalBuffer *buf;
  302. int buf_size, ret;
  303. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  304. frame->nb_samples, avctx->sample_fmt,
  305. 0);
  306. if (buf_size < 0)
  307. return AVERROR(EINVAL);
  308. /* allocate InternalBuffer if needed */
  309. if (!avci->buffer) {
  310. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  311. if (!avci->buffer)
  312. return AVERROR(ENOMEM);
  313. }
  314. buf = avci->buffer;
  315. /* if there is a previously-used internal buffer, check its size and
  316. channel count to see if we can reuse it */
  317. if (buf->extended_data) {
  318. /* if current buffer is too small, free it */
  319. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  320. av_free(buf->extended_data[0]);
  321. if (buf->extended_data != buf->data)
  322. av_freep(&buf->extended_data);
  323. buf->extended_data = NULL;
  324. buf->data[0] = NULL;
  325. }
  326. /* if number of channels has changed, reset and/or free extended data
  327. pointers but leave data buffer in buf->data[0] for reuse */
  328. if (buf->nb_channels != avctx->channels) {
  329. if (buf->extended_data != buf->data)
  330. av_free(buf->extended_data);
  331. buf->extended_data = NULL;
  332. }
  333. }
  334. /* if there is no previous buffer or the previous buffer cannot be used
  335. as-is, allocate a new buffer and/or rearrange the channel pointers */
  336. if (!buf->extended_data) {
  337. if (!buf->data[0]) {
  338. if (!(buf->data[0] = av_mallocz(buf_size)))
  339. return AVERROR(ENOMEM);
  340. buf->audio_data_size = buf_size;
  341. }
  342. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  343. avctx->sample_fmt, buf->data[0],
  344. buf->audio_data_size, 0)))
  345. return ret;
  346. if (frame->extended_data == frame->data)
  347. buf->extended_data = buf->data;
  348. else
  349. buf->extended_data = frame->extended_data;
  350. memcpy(buf->data, frame->data, sizeof(frame->data));
  351. buf->linesize[0] = frame->linesize[0];
  352. buf->nb_channels = avctx->channels;
  353. } else {
  354. /* copy InternalBuffer info to the AVFrame */
  355. frame->extended_data = buf->extended_data;
  356. frame->linesize[0] = buf->linesize[0];
  357. memcpy(frame->data, buf->data, sizeof(frame->data));
  358. }
  359. frame->type = FF_BUFFER_TYPE_INTERNAL;
  360. if (avctx->pkt) {
  361. frame->pkt_pts = avctx->pkt->pts;
  362. frame->pkt_pos = avctx->pkt->pos;
  363. frame->pkt_duration = avctx->pkt->duration;
  364. } else {
  365. frame->pkt_pts = AV_NOPTS_VALUE;
  366. frame->pkt_pos = -1;
  367. frame->pkt_duration = 0;
  368. }
  369. frame->reordered_opaque = avctx->reordered_opaque;
  370. frame->sample_rate = avctx->sample_rate;
  371. frame->format = avctx->sample_fmt;
  372. frame->channel_layout = avctx->channel_layout;
  373. if (avctx->debug & FF_DEBUG_BUFFERS)
  374. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  375. "internal audio buffer used\n", frame);
  376. return 0;
  377. }
  378. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  379. {
  380. int i;
  381. int w= s->width;
  382. int h= s->height;
  383. InternalBuffer *buf;
  384. AVCodecInternal *avci = s->internal;
  385. if(pic->data[0]!=NULL) {
  386. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  387. return -1;
  388. }
  389. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  390. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  391. return -1;
  392. }
  393. if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0)
  394. return -1;
  395. if (!avci->buffer) {
  396. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  397. sizeof(InternalBuffer));
  398. }
  399. buf = &avci->buffer[avci->buffer_count];
  400. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  401. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  402. av_freep(&buf->base[i]);
  403. buf->data[i]= NULL;
  404. }
  405. }
  406. if (!buf->base[0]) {
  407. int h_chroma_shift, v_chroma_shift;
  408. int size[4] = {0};
  409. int tmpsize;
  410. int unaligned;
  411. AVPicture picture;
  412. int stride_align[AV_NUM_DATA_POINTERS];
  413. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  414. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  415. avcodec_align_dimensions2(s, &w, &h, stride_align);
  416. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  417. w+= EDGE_WIDTH*2;
  418. h+= EDGE_WIDTH*2;
  419. }
  420. do {
  421. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  422. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  423. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  424. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  425. w += w & ~(w-1);
  426. unaligned = 0;
  427. for (i=0; i<4; i++){
  428. unaligned |= picture.linesize[i] % stride_align[i];
  429. }
  430. } while (unaligned);
  431. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  432. if (tmpsize < 0)
  433. return -1;
  434. for (i=0; i<3 && picture.data[i+1]; i++)
  435. size[i] = picture.data[i+1] - picture.data[i];
  436. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  437. memset(buf->base, 0, sizeof(buf->base));
  438. memset(buf->data, 0, sizeof(buf->data));
  439. for(i=0; i<4 && size[i]; i++){
  440. const int h_shift= i==0 ? 0 : h_chroma_shift;
  441. const int v_shift= i==0 ? 0 : v_chroma_shift;
  442. buf->linesize[i]= picture.linesize[i];
  443. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  444. if(buf->base[i]==NULL) return -1;
  445. memset(buf->base[i], 128, size[i]);
  446. // no edge if EDGE EMU or not planar YUV
  447. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  448. buf->data[i] = buf->base[i];
  449. else
  450. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  451. }
  452. for (; i < AV_NUM_DATA_POINTERS; i++) {
  453. buf->base[i] = buf->data[i] = NULL;
  454. buf->linesize[i] = 0;
  455. }
  456. if(size[1] && !size[2])
  457. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  458. buf->width = s->width;
  459. buf->height = s->height;
  460. buf->pix_fmt= s->pix_fmt;
  461. }
  462. pic->type= FF_BUFFER_TYPE_INTERNAL;
  463. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  464. pic->base[i]= buf->base[i];
  465. pic->data[i]= buf->data[i];
  466. pic->linesize[i]= buf->linesize[i];
  467. }
  468. pic->extended_data = pic->data;
  469. avci->buffer_count++;
  470. pic->width = buf->width;
  471. pic->height = buf->height;
  472. pic->format = buf->pix_fmt;
  473. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  474. if (s->pkt) {
  475. pic->pkt_pts = s->pkt->pts;
  476. pic->pkt_pos = s->pkt->pos;
  477. pic->pkt_duration = s->pkt->duration;
  478. } else {
  479. pic->pkt_pts = AV_NOPTS_VALUE;
  480. pic->pkt_pos = -1;
  481. pic->pkt_duration = 0;
  482. }
  483. pic->reordered_opaque= s->reordered_opaque;
  484. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  485. pic->width = s->width;
  486. pic->height = s->height;
  487. pic->format = s->pix_fmt;
  488. if(s->debug&FF_DEBUG_BUFFERS)
  489. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  490. "buffers used\n", pic, avci->buffer_count);
  491. return 0;
  492. }
  493. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  494. {
  495. switch (avctx->codec_type) {
  496. case AVMEDIA_TYPE_VIDEO:
  497. return video_get_buffer(avctx, frame);
  498. case AVMEDIA_TYPE_AUDIO:
  499. return audio_get_buffer(avctx, frame);
  500. default:
  501. return -1;
  502. }
  503. }
  504. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  505. int i;
  506. InternalBuffer *buf, *last;
  507. AVCodecInternal *avci = s->internal;
  508. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  509. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  510. assert(avci->buffer_count);
  511. if (avci->buffer) {
  512. buf = NULL; /* avoids warning */
  513. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  514. buf = &avci->buffer[i];
  515. if (buf->data[0] == pic->data[0])
  516. break;
  517. }
  518. av_assert0(i < avci->buffer_count);
  519. avci->buffer_count--;
  520. last = &avci->buffer[avci->buffer_count];
  521. if (buf != last)
  522. FFSWAP(InternalBuffer, *buf, *last);
  523. }
  524. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  525. pic->data[i]=NULL;
  526. // pic->base[i]=NULL;
  527. }
  528. //printf("R%X\n", pic->opaque);
  529. if(s->debug&FF_DEBUG_BUFFERS)
  530. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  531. "buffers used\n", pic, avci->buffer_count);
  532. }
  533. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  534. AVFrame temp_pic;
  535. int i;
  536. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  537. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  538. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  539. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  540. s->release_buffer(s, pic);
  541. }
  542. ff_init_buffer_info(s, pic);
  543. /* If no picture return a new buffer */
  544. if(pic->data[0] == NULL) {
  545. /* We will copy from buffer, so must be readable */
  546. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  547. return s->get_buffer(s, pic);
  548. }
  549. assert(s->pix_fmt == pic->format);
  550. /* If internal buffer type return the same buffer */
  551. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  552. return 0;
  553. }
  554. /*
  555. * Not internal type and reget_buffer not overridden, emulate cr buffer
  556. */
  557. temp_pic = *pic;
  558. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  559. pic->data[i] = pic->base[i] = NULL;
  560. pic->opaque = NULL;
  561. /* Allocate new frame */
  562. if (s->get_buffer(s, pic))
  563. return -1;
  564. /* Copy image data from old buffer to new buffer */
  565. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  566. s->height);
  567. s->release_buffer(s, &temp_pic); // Release old frame
  568. return 0;
  569. }
  570. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  571. int i;
  572. for(i=0; i<count; i++){
  573. int r= func(c, (char*)arg + i*size);
  574. if(ret) ret[i]= r;
  575. }
  576. return 0;
  577. }
  578. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  579. int i;
  580. for(i=0; i<count; i++){
  581. int r= func(c, arg, i, 0);
  582. if(ret) ret[i]= r;
  583. }
  584. return 0;
  585. }
  586. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  587. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  588. ++fmt;
  589. return fmt[0];
  590. }
  591. void avcodec_get_frame_defaults(AVFrame *pic){
  592. memset(pic, 0, sizeof(AVFrame));
  593. pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  594. pic->pkt_duration = 0;
  595. pic->pkt_pos = -1;
  596. pic->key_frame= 1;
  597. pic->sample_aspect_ratio = (AVRational){0, 1};
  598. pic->format = -1; /* unknown */
  599. }
  600. AVFrame *avcodec_alloc_frame(void){
  601. AVFrame *pic= av_malloc(sizeof(AVFrame));
  602. if(pic==NULL) return NULL;
  603. avcodec_get_frame_defaults(pic);
  604. return pic;
  605. }
  606. #define MAKE_ACCESSORS(str, name, type, field) \
  607. type av_##name##_get_##field(const str *s) { return s->field; } \
  608. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  609. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  610. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  611. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  612. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  613. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  614. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  615. {
  616. memset(sub, 0, sizeof(*sub));
  617. sub->pts = AV_NOPTS_VALUE;
  618. }
  619. static int get_bit_rate(AVCodecContext *ctx)
  620. {
  621. int bit_rate;
  622. int bits_per_sample;
  623. switch(ctx->codec_type) {
  624. case AVMEDIA_TYPE_VIDEO:
  625. case AVMEDIA_TYPE_DATA:
  626. case AVMEDIA_TYPE_SUBTITLE:
  627. case AVMEDIA_TYPE_ATTACHMENT:
  628. bit_rate = ctx->bit_rate;
  629. break;
  630. case AVMEDIA_TYPE_AUDIO:
  631. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  632. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  633. break;
  634. default:
  635. bit_rate = 0;
  636. break;
  637. }
  638. return bit_rate;
  639. }
  640. #if FF_API_AVCODEC_OPEN
  641. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  642. {
  643. return avcodec_open2(avctx, codec, NULL);
  644. }
  645. #endif
  646. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  647. {
  648. int ret = 0;
  649. AVDictionary *tmp = NULL;
  650. if (avcodec_is_open(avctx))
  651. return 0;
  652. if ((!codec && !avctx->codec)) {
  653. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  654. return AVERROR(EINVAL);
  655. }
  656. if ((codec && avctx->codec && codec != avctx->codec)) {
  657. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  658. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  659. return AVERROR(EINVAL);
  660. }
  661. if (!codec)
  662. codec = avctx->codec;
  663. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  664. return AVERROR(EINVAL);
  665. if (options)
  666. av_dict_copy(&tmp, *options, 0);
  667. /* If there is a user-supplied mutex locking routine, call it. */
  668. if (ff_lockmgr_cb) {
  669. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  670. return -1;
  671. }
  672. entangled_thread_counter++;
  673. if(entangled_thread_counter != 1){
  674. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  675. ret = -1;
  676. goto end;
  677. }
  678. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  679. if (!avctx->internal) {
  680. ret = AVERROR(ENOMEM);
  681. goto end;
  682. }
  683. if (codec->priv_data_size > 0) {
  684. if(!avctx->priv_data){
  685. avctx->priv_data = av_mallocz(codec->priv_data_size);
  686. if (!avctx->priv_data) {
  687. ret = AVERROR(ENOMEM);
  688. goto end;
  689. }
  690. if (codec->priv_class) {
  691. *(const AVClass**)avctx->priv_data= codec->priv_class;
  692. av_opt_set_defaults(avctx->priv_data);
  693. }
  694. }
  695. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  696. goto free_and_end;
  697. } else {
  698. avctx->priv_data = NULL;
  699. }
  700. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  701. goto free_and_end;
  702. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  703. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  704. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
  705. ret = -1;
  706. goto free_and_end;
  707. }
  708. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  709. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  710. if(avctx->coded_width && avctx->coded_height)
  711. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  712. else if(avctx->width && avctx->height)
  713. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  714. }
  715. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  716. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  717. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  718. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  719. avcodec_set_dimensions(avctx, 0, 0);
  720. }
  721. /* if the decoder init function was already called previously,
  722. free the already allocated subtitle_header before overwriting it */
  723. if (av_codec_is_decoder(codec))
  724. av_freep(&avctx->subtitle_header);
  725. #define SANE_NB_CHANNELS 128U
  726. if (avctx->channels > SANE_NB_CHANNELS) {
  727. ret = AVERROR(EINVAL);
  728. goto free_and_end;
  729. }
  730. avctx->codec = codec;
  731. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  732. avctx->codec_id == CODEC_ID_NONE) {
  733. avctx->codec_type = codec->type;
  734. avctx->codec_id = codec->id;
  735. }
  736. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  737. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  738. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  739. ret = AVERROR(EINVAL);
  740. goto free_and_end;
  741. }
  742. avctx->frame_number = 0;
  743. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  744. (!avctx->time_base.num || !avctx->time_base.den)) {
  745. avctx->time_base.num = 1;
  746. avctx->time_base.den = avctx->sample_rate;
  747. }
  748. if (!HAVE_THREADS)
  749. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  750. entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  751. ret = ff_frame_thread_encoder_init(avctx);
  752. entangled_thread_counter++;
  753. if (ret < 0)
  754. goto free_and_end;
  755. if (HAVE_THREADS && !avctx->thread_opaque
  756. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  757. ret = ff_thread_init(avctx);
  758. if (ret < 0) {
  759. goto free_and_end;
  760. }
  761. }
  762. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  763. avctx->thread_count = 1;
  764. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  765. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  766. avctx->codec->max_lowres);
  767. ret = AVERROR(EINVAL);
  768. goto free_and_end;
  769. }
  770. if (av_codec_is_encoder(avctx->codec)) {
  771. int i;
  772. if (avctx->codec->sample_fmts) {
  773. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  774. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  775. break;
  776. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  777. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  778. ret = AVERROR(EINVAL);
  779. goto free_and_end;
  780. }
  781. }
  782. if (avctx->codec->pix_fmts) {
  783. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  784. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  785. break;
  786. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE
  787. && !((avctx->codec_id == CODEC_ID_MJPEG || avctx->codec_id == CODEC_ID_LJPEG)
  788. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  789. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  790. ret = AVERROR(EINVAL);
  791. goto free_and_end;
  792. }
  793. }
  794. if (avctx->codec->supported_samplerates) {
  795. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  796. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  797. break;
  798. if (avctx->codec->supported_samplerates[i] == 0) {
  799. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  800. ret = AVERROR(EINVAL);
  801. goto free_and_end;
  802. }
  803. }
  804. if (avctx->codec->channel_layouts) {
  805. if (!avctx->channel_layout) {
  806. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  807. } else {
  808. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  809. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  810. break;
  811. if (avctx->codec->channel_layouts[i] == 0) {
  812. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  813. ret = AVERROR(EINVAL);
  814. goto free_and_end;
  815. }
  816. }
  817. }
  818. if (avctx->channel_layout && avctx->channels) {
  819. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  820. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  821. ret = AVERROR(EINVAL);
  822. goto free_and_end;
  823. }
  824. } else if (avctx->channel_layout) {
  825. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  826. }
  827. }
  828. avctx->pts_correction_num_faulty_pts =
  829. avctx->pts_correction_num_faulty_dts = 0;
  830. avctx->pts_correction_last_pts =
  831. avctx->pts_correction_last_dts = INT64_MIN;
  832. if(avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME) || avctx->internal->frame_thread_encoder)){
  833. ret = avctx->codec->init(avctx);
  834. if (ret < 0) {
  835. goto free_and_end;
  836. }
  837. }
  838. ret=0;
  839. if (av_codec_is_decoder(avctx->codec)) {
  840. if (!avctx->bit_rate)
  841. avctx->bit_rate = get_bit_rate(avctx);
  842. /* validate channel layout from the decoder */
  843. if (avctx->channel_layout &&
  844. av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  845. av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
  846. avctx->channel_layout = 0;
  847. }
  848. }
  849. end:
  850. entangled_thread_counter--;
  851. /* Release any user-supplied mutex. */
  852. if (ff_lockmgr_cb) {
  853. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  854. }
  855. if (options) {
  856. av_dict_free(options);
  857. *options = tmp;
  858. }
  859. return ret;
  860. free_and_end:
  861. av_dict_free(&tmp);
  862. av_freep(&avctx->priv_data);
  863. av_freep(&avctx->internal);
  864. avctx->codec= NULL;
  865. goto end;
  866. }
  867. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  868. {
  869. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  870. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  871. return AVERROR(EINVAL);
  872. }
  873. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  874. if (!avpkt->data || avpkt->size < size) {
  875. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  876. avpkt->data = avctx->internal->byte_buffer;
  877. avpkt->size = avctx->internal->byte_buffer_size;
  878. avpkt->destruct = NULL;
  879. }
  880. if (avpkt->data) {
  881. void *destruct = avpkt->destruct;
  882. if (avpkt->size < size) {
  883. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  884. return AVERROR(EINVAL);
  885. }
  886. av_init_packet(avpkt);
  887. avpkt->destruct = destruct;
  888. avpkt->size = size;
  889. return 0;
  890. } else {
  891. int ret = av_new_packet(avpkt, size);
  892. if (ret < 0)
  893. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  894. return ret;
  895. }
  896. }
  897. int ff_alloc_packet(AVPacket *avpkt, int size)
  898. {
  899. return ff_alloc_packet2(NULL, avpkt, size);
  900. }
  901. /**
  902. * Pad last frame with silence.
  903. */
  904. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  905. {
  906. AVFrame *frame = NULL;
  907. uint8_t *buf = NULL;
  908. int ret;
  909. if (!(frame = avcodec_alloc_frame()))
  910. return AVERROR(ENOMEM);
  911. *frame = *src;
  912. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  913. s->frame_size, s->sample_fmt, 0)) < 0)
  914. goto fail;
  915. if (!(buf = av_malloc(ret))) {
  916. ret = AVERROR(ENOMEM);
  917. goto fail;
  918. }
  919. frame->nb_samples = s->frame_size;
  920. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  921. buf, ret, 0)) < 0)
  922. goto fail;
  923. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  924. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  925. goto fail;
  926. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  927. frame->nb_samples - src->nb_samples,
  928. s->channels, s->sample_fmt)) < 0)
  929. goto fail;
  930. *dst = frame;
  931. return 0;
  932. fail:
  933. if (frame->extended_data != frame->data)
  934. av_freep(&frame->extended_data);
  935. av_freep(&buf);
  936. av_freep(&frame);
  937. return ret;
  938. }
  939. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  940. AVPacket *avpkt,
  941. const AVFrame *frame,
  942. int *got_packet_ptr)
  943. {
  944. AVFrame tmp;
  945. AVFrame *padded_frame = NULL;
  946. int ret;
  947. AVPacket user_pkt = *avpkt;
  948. int needs_realloc = !user_pkt.data;
  949. *got_packet_ptr = 0;
  950. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  951. av_free_packet(avpkt);
  952. av_init_packet(avpkt);
  953. return 0;
  954. }
  955. /* ensure that extended_data is properly set */
  956. if (frame && !frame->extended_data) {
  957. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  958. avctx->channels > AV_NUM_DATA_POINTERS) {
  959. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  960. "with more than %d channels, but extended_data is not set.\n",
  961. AV_NUM_DATA_POINTERS);
  962. return AVERROR(EINVAL);
  963. }
  964. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  965. tmp = *frame;
  966. tmp.extended_data = tmp.data;
  967. frame = &tmp;
  968. }
  969. /* check for valid frame size */
  970. if (frame) {
  971. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  972. if (frame->nb_samples > avctx->frame_size) {
  973. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  974. return AVERROR(EINVAL);
  975. }
  976. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  977. if (frame->nb_samples < avctx->frame_size &&
  978. !avctx->internal->last_audio_frame) {
  979. ret = pad_last_frame(avctx, &padded_frame, frame);
  980. if (ret < 0)
  981. return ret;
  982. frame = padded_frame;
  983. avctx->internal->last_audio_frame = 1;
  984. }
  985. if (frame->nb_samples != avctx->frame_size) {
  986. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  987. return AVERROR(EINVAL);
  988. }
  989. }
  990. }
  991. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  992. if (!ret) {
  993. if (*got_packet_ptr) {
  994. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  995. if (avpkt->pts == AV_NOPTS_VALUE)
  996. avpkt->pts = frame->pts;
  997. if (!avpkt->duration)
  998. avpkt->duration = ff_samples_to_time_base(avctx,
  999. frame->nb_samples);
  1000. }
  1001. avpkt->dts = avpkt->pts;
  1002. } else {
  1003. avpkt->size = 0;
  1004. }
  1005. }
  1006. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1007. needs_realloc = 0;
  1008. if (user_pkt.data) {
  1009. if (user_pkt.size >= avpkt->size) {
  1010. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1011. } else {
  1012. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1013. avpkt->size = user_pkt.size;
  1014. ret = -1;
  1015. }
  1016. avpkt->data = user_pkt.data;
  1017. avpkt->destruct = user_pkt.destruct;
  1018. } else {
  1019. if (av_dup_packet(avpkt) < 0) {
  1020. ret = AVERROR(ENOMEM);
  1021. }
  1022. }
  1023. }
  1024. if (!ret) {
  1025. if (needs_realloc && avpkt->data) {
  1026. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1027. if (new_data)
  1028. avpkt->data = new_data;
  1029. }
  1030. avctx->frame_number++;
  1031. }
  1032. if (ret < 0 || !*got_packet_ptr) {
  1033. av_free_packet(avpkt);
  1034. av_init_packet(avpkt);
  1035. return ret;
  1036. }
  1037. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1038. this needs to be moved to the encoders, but for now we can do it
  1039. here to simplify things */
  1040. avpkt->flags |= AV_PKT_FLAG_KEY;
  1041. if (padded_frame) {
  1042. av_freep(&padded_frame->data[0]);
  1043. if (padded_frame->extended_data != padded_frame->data)
  1044. av_freep(&padded_frame->extended_data);
  1045. av_freep(&padded_frame);
  1046. }
  1047. return ret;
  1048. }
  1049. #if FF_API_OLD_DECODE_AUDIO
  1050. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1051. uint8_t *buf, int buf_size,
  1052. const short *samples)
  1053. {
  1054. AVPacket pkt;
  1055. AVFrame frame0;
  1056. AVFrame *frame;
  1057. int ret, samples_size, got_packet;
  1058. av_init_packet(&pkt);
  1059. pkt.data = buf;
  1060. pkt.size = buf_size;
  1061. if (samples) {
  1062. frame = &frame0;
  1063. avcodec_get_frame_defaults(frame);
  1064. if (avctx->frame_size) {
  1065. frame->nb_samples = avctx->frame_size;
  1066. } else {
  1067. /* if frame_size is not set, the number of samples must be
  1068. calculated from the buffer size */
  1069. int64_t nb_samples;
  1070. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1071. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1072. "support this codec\n");
  1073. return AVERROR(EINVAL);
  1074. }
  1075. nb_samples = (int64_t)buf_size * 8 /
  1076. (av_get_bits_per_sample(avctx->codec_id) *
  1077. avctx->channels);
  1078. if (nb_samples >= INT_MAX)
  1079. return AVERROR(EINVAL);
  1080. frame->nb_samples = nb_samples;
  1081. }
  1082. /* it is assumed that the samples buffer is large enough based on the
  1083. relevant parameters */
  1084. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1085. frame->nb_samples,
  1086. avctx->sample_fmt, 1);
  1087. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1088. avctx->sample_fmt,
  1089. (const uint8_t *) samples,
  1090. samples_size, 1)))
  1091. return ret;
  1092. /* fabricate frame pts from sample count.
  1093. this is needed because the avcodec_encode_audio() API does not have
  1094. a way for the user to provide pts */
  1095. if(avctx->sample_rate && avctx->time_base.num)
  1096. frame->pts = ff_samples_to_time_base(avctx,
  1097. avctx->internal->sample_count);
  1098. else
  1099. frame->pts = AV_NOPTS_VALUE;
  1100. avctx->internal->sample_count += frame->nb_samples;
  1101. } else {
  1102. frame = NULL;
  1103. }
  1104. got_packet = 0;
  1105. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1106. if (!ret && got_packet && avctx->coded_frame) {
  1107. avctx->coded_frame->pts = pkt.pts;
  1108. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1109. }
  1110. /* free any side data since we cannot return it */
  1111. ff_packet_free_side_data(&pkt);
  1112. if (frame && frame->extended_data != frame->data)
  1113. av_freep(&frame->extended_data);
  1114. return ret ? ret : pkt.size;
  1115. }
  1116. #endif
  1117. #if FF_API_OLD_ENCODE_VIDEO
  1118. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1119. const AVFrame *pict)
  1120. {
  1121. AVPacket pkt;
  1122. int ret, got_packet = 0;
  1123. if(buf_size < FF_MIN_BUFFER_SIZE){
  1124. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1125. return -1;
  1126. }
  1127. av_init_packet(&pkt);
  1128. pkt.data = buf;
  1129. pkt.size = buf_size;
  1130. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1131. if (!ret && got_packet && avctx->coded_frame) {
  1132. avctx->coded_frame->pts = pkt.pts;
  1133. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1134. }
  1135. /* free any side data since we cannot return it */
  1136. if (pkt.side_data_elems > 0) {
  1137. int i;
  1138. for (i = 0; i < pkt.side_data_elems; i++)
  1139. av_free(pkt.side_data[i].data);
  1140. av_freep(&pkt.side_data);
  1141. pkt.side_data_elems = 0;
  1142. }
  1143. return ret ? ret : pkt.size;
  1144. }
  1145. #endif
  1146. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1147. AVPacket *avpkt,
  1148. const AVFrame *frame,
  1149. int *got_packet_ptr)
  1150. {
  1151. int ret;
  1152. AVPacket user_pkt = *avpkt;
  1153. int needs_realloc = !user_pkt.data;
  1154. *got_packet_ptr = 0;
  1155. if(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1156. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1157. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1158. av_free_packet(avpkt);
  1159. av_init_packet(avpkt);
  1160. avpkt->size = 0;
  1161. return 0;
  1162. }
  1163. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1164. return AVERROR(EINVAL);
  1165. av_assert0(avctx->codec->encode2);
  1166. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1167. av_assert0(ret <= 0);
  1168. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1169. needs_realloc = 0;
  1170. if (user_pkt.data) {
  1171. if (user_pkt.size >= avpkt->size) {
  1172. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1173. } else {
  1174. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1175. avpkt->size = user_pkt.size;
  1176. ret = -1;
  1177. }
  1178. avpkt->data = user_pkt.data;
  1179. avpkt->destruct = user_pkt.destruct;
  1180. } else {
  1181. if (av_dup_packet(avpkt) < 0) {
  1182. ret = AVERROR(ENOMEM);
  1183. }
  1184. }
  1185. }
  1186. if (!ret) {
  1187. if (!*got_packet_ptr)
  1188. avpkt->size = 0;
  1189. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1190. avpkt->pts = avpkt->dts = frame->pts;
  1191. if (needs_realloc && avpkt->data &&
  1192. avpkt->destruct == av_destruct_packet) {
  1193. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1194. if (new_data)
  1195. avpkt->data = new_data;
  1196. }
  1197. avctx->frame_number++;
  1198. }
  1199. if (ret < 0 || !*got_packet_ptr)
  1200. av_free_packet(avpkt);
  1201. emms_c();
  1202. return ret;
  1203. }
  1204. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1205. const AVSubtitle *sub)
  1206. {
  1207. int ret;
  1208. if(sub->start_display_time) {
  1209. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1210. return -1;
  1211. }
  1212. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
  1213. avctx->frame_number++;
  1214. return ret;
  1215. }
  1216. /**
  1217. * Attempt to guess proper monotonic timestamps for decoded video frames
  1218. * which might have incorrect times. Input timestamps may wrap around, in
  1219. * which case the output will as well.
  1220. *
  1221. * @param pts the pts field of the decoded AVPacket, as passed through
  1222. * AVFrame.pkt_pts
  1223. * @param dts the dts field of the decoded AVPacket
  1224. * @return one of the input values, may be AV_NOPTS_VALUE
  1225. */
  1226. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1227. int64_t reordered_pts, int64_t dts)
  1228. {
  1229. int64_t pts = AV_NOPTS_VALUE;
  1230. if (dts != AV_NOPTS_VALUE) {
  1231. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1232. ctx->pts_correction_last_dts = dts;
  1233. }
  1234. if (reordered_pts != AV_NOPTS_VALUE) {
  1235. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1236. ctx->pts_correction_last_pts = reordered_pts;
  1237. }
  1238. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1239. && reordered_pts != AV_NOPTS_VALUE)
  1240. pts = reordered_pts;
  1241. else
  1242. pts = dts;
  1243. return pts;
  1244. }
  1245. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1246. {
  1247. int size = 0;
  1248. const uint8_t *data;
  1249. uint32_t flags;
  1250. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1251. return;
  1252. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1253. if (!data || size < 4)
  1254. return;
  1255. flags = bytestream_get_le32(&data);
  1256. size -= 4;
  1257. if (size < 4) /* Required for any of the changes */
  1258. return;
  1259. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1260. avctx->channels = bytestream_get_le32(&data);
  1261. size -= 4;
  1262. }
  1263. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1264. if (size < 8)
  1265. return;
  1266. avctx->channel_layout = bytestream_get_le64(&data);
  1267. size -= 8;
  1268. }
  1269. if (size < 4)
  1270. return;
  1271. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1272. avctx->sample_rate = bytestream_get_le32(&data);
  1273. size -= 4;
  1274. }
  1275. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1276. if (size < 8)
  1277. return;
  1278. avctx->width = bytestream_get_le32(&data);
  1279. avctx->height = bytestream_get_le32(&data);
  1280. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1281. size -= 8;
  1282. }
  1283. }
  1284. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1285. int *got_picture_ptr,
  1286. const AVPacket *avpkt)
  1287. {
  1288. int ret;
  1289. // copy to ensure we do not change avpkt
  1290. AVPacket tmp = *avpkt;
  1291. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1292. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1293. return AVERROR(EINVAL);
  1294. }
  1295. *got_picture_ptr= 0;
  1296. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1297. return AVERROR(EINVAL);
  1298. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1299. int did_split = av_packet_split_side_data(&tmp);
  1300. apply_param_change(avctx, &tmp);
  1301. avctx->pkt = &tmp;
  1302. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1303. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1304. &tmp);
  1305. else {
  1306. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1307. &tmp);
  1308. picture->pkt_dts= avpkt->dts;
  1309. if(!avctx->has_b_frames){
  1310. picture->pkt_pos= avpkt->pos;
  1311. }
  1312. //FIXME these should be under if(!avctx->has_b_frames)
  1313. if (!picture->sample_aspect_ratio.num)
  1314. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1315. if (!picture->width)
  1316. picture->width = avctx->width;
  1317. if (!picture->height)
  1318. picture->height = avctx->height;
  1319. if (picture->format == PIX_FMT_NONE)
  1320. picture->format = avctx->pix_fmt;
  1321. }
  1322. emms_c(); //needed to avoid an emms_c() call before every return;
  1323. avctx->pkt = NULL;
  1324. if (did_split) {
  1325. ff_packet_free_side_data(&tmp);
  1326. if(ret == tmp.size)
  1327. ret = avpkt->size;
  1328. }
  1329. if (*got_picture_ptr){
  1330. avctx->frame_number++;
  1331. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1332. picture->pkt_pts,
  1333. picture->pkt_dts);
  1334. }
  1335. }else
  1336. ret= 0;
  1337. return ret;
  1338. }
  1339. #if FF_API_OLD_DECODE_AUDIO
  1340. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1341. int *frame_size_ptr,
  1342. AVPacket *avpkt)
  1343. {
  1344. AVFrame frame;
  1345. int ret, got_frame = 0;
  1346. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1347. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1348. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1349. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1350. "avcodec_decode_audio4()\n");
  1351. avctx->get_buffer = avcodec_default_get_buffer;
  1352. avctx->release_buffer = avcodec_default_release_buffer;
  1353. }
  1354. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1355. if (ret >= 0 && got_frame) {
  1356. int ch, plane_size;
  1357. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1358. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1359. frame.nb_samples,
  1360. avctx->sample_fmt, 1);
  1361. if (*frame_size_ptr < data_size) {
  1362. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1363. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1364. return AVERROR(EINVAL);
  1365. }
  1366. memcpy(samples, frame.extended_data[0], plane_size);
  1367. if (planar && avctx->channels > 1) {
  1368. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1369. for (ch = 1; ch < avctx->channels; ch++) {
  1370. memcpy(out, frame.extended_data[ch], plane_size);
  1371. out += plane_size;
  1372. }
  1373. }
  1374. *frame_size_ptr = data_size;
  1375. } else {
  1376. *frame_size_ptr = 0;
  1377. }
  1378. return ret;
  1379. }
  1380. #endif
  1381. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1382. AVFrame *frame,
  1383. int *got_frame_ptr,
  1384. const AVPacket *avpkt)
  1385. {
  1386. int ret = 0;
  1387. *got_frame_ptr = 0;
  1388. if (!avpkt->data && avpkt->size) {
  1389. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1390. return AVERROR(EINVAL);
  1391. }
  1392. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1393. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1394. return AVERROR(EINVAL);
  1395. }
  1396. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1397. // copy to ensure we do not change avpkt
  1398. AVPacket tmp = *avpkt;
  1399. int did_split = av_packet_split_side_data(&tmp);
  1400. apply_param_change(avctx, &tmp);
  1401. avctx->pkt = &tmp;
  1402. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1403. if (ret >= 0 && *got_frame_ptr) {
  1404. avctx->frame_number++;
  1405. frame->pkt_dts = avpkt->dts;
  1406. if (frame->format == AV_SAMPLE_FMT_NONE)
  1407. frame->format = avctx->sample_fmt;
  1408. if (!frame->channel_layout)
  1409. frame->channel_layout = avctx->channel_layout;
  1410. if (!frame->sample_rate)
  1411. frame->sample_rate = avctx->sample_rate;
  1412. }
  1413. avctx->pkt = NULL;
  1414. if (did_split) {
  1415. ff_packet_free_side_data(&tmp);
  1416. if(ret == tmp.size)
  1417. ret = avpkt->size;
  1418. }
  1419. }
  1420. return ret;
  1421. }
  1422. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1423. int *got_sub_ptr,
  1424. AVPacket *avpkt)
  1425. {
  1426. int ret;
  1427. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1428. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1429. return AVERROR(EINVAL);
  1430. }
  1431. avctx->pkt = avpkt;
  1432. *got_sub_ptr = 0;
  1433. avcodec_get_subtitle_defaults(sub);
  1434. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1435. if (*got_sub_ptr)
  1436. avctx->frame_number++;
  1437. return ret;
  1438. }
  1439. void avsubtitle_free(AVSubtitle *sub)
  1440. {
  1441. int i;
  1442. for (i = 0; i < sub->num_rects; i++)
  1443. {
  1444. av_freep(&sub->rects[i]->pict.data[0]);
  1445. av_freep(&sub->rects[i]->pict.data[1]);
  1446. av_freep(&sub->rects[i]->pict.data[2]);
  1447. av_freep(&sub->rects[i]->pict.data[3]);
  1448. av_freep(&sub->rects[i]->text);
  1449. av_freep(&sub->rects[i]->ass);
  1450. av_freep(&sub->rects[i]);
  1451. }
  1452. av_freep(&sub->rects);
  1453. memset(sub, 0, sizeof(AVSubtitle));
  1454. }
  1455. av_cold int avcodec_close(AVCodecContext *avctx)
  1456. {
  1457. /* If there is a user-supplied mutex locking routine, call it. */
  1458. if (ff_lockmgr_cb) {
  1459. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1460. return -1;
  1461. }
  1462. entangled_thread_counter++;
  1463. if(entangled_thread_counter != 1){
  1464. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1465. entangled_thread_counter--;
  1466. return -1;
  1467. }
  1468. if (avcodec_is_open(avctx)) {
  1469. if (avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1470. entangled_thread_counter --;
  1471. ff_frame_thread_encoder_free(avctx);
  1472. entangled_thread_counter ++;
  1473. }
  1474. if (HAVE_THREADS && avctx->thread_opaque)
  1475. ff_thread_free(avctx);
  1476. if (avctx->codec && avctx->codec->close)
  1477. avctx->codec->close(avctx);
  1478. avcodec_default_free_buffers(avctx);
  1479. avctx->coded_frame = NULL;
  1480. avctx->internal->byte_buffer_size = 0;
  1481. av_freep(&avctx->internal->byte_buffer);
  1482. av_freep(&avctx->internal);
  1483. }
  1484. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1485. av_opt_free(avctx->priv_data);
  1486. av_opt_free(avctx);
  1487. av_freep(&avctx->priv_data);
  1488. if (av_codec_is_encoder(avctx->codec))
  1489. av_freep(&avctx->extradata);
  1490. avctx->codec = NULL;
  1491. avctx->active_thread_type = 0;
  1492. entangled_thread_counter--;
  1493. /* Release any user-supplied mutex. */
  1494. if (ff_lockmgr_cb) {
  1495. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1496. }
  1497. return 0;
  1498. }
  1499. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1500. {
  1501. switch(id){
  1502. //This is for future deprecatec codec ids, its empty since
  1503. //last major bump but will fill up again over time, please don't remove it
  1504. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1505. default : return id;
  1506. }
  1507. }
  1508. AVCodec *avcodec_find_encoder(enum CodecID id)
  1509. {
  1510. AVCodec *p, *experimental=NULL;
  1511. p = first_avcodec;
  1512. id= remap_deprecated_codec_id(id);
  1513. while (p) {
  1514. if (av_codec_is_encoder(p) && p->id == id) {
  1515. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1516. experimental = p;
  1517. } else
  1518. return p;
  1519. }
  1520. p = p->next;
  1521. }
  1522. return experimental;
  1523. }
  1524. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1525. {
  1526. AVCodec *p;
  1527. if (!name)
  1528. return NULL;
  1529. p = first_avcodec;
  1530. while (p) {
  1531. if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1532. return p;
  1533. p = p->next;
  1534. }
  1535. return NULL;
  1536. }
  1537. AVCodec *avcodec_find_decoder(enum CodecID id)
  1538. {
  1539. AVCodec *p, *experimental=NULL;
  1540. p = first_avcodec;
  1541. id= remap_deprecated_codec_id(id);
  1542. while (p) {
  1543. if (av_codec_is_decoder(p) && p->id == id) {
  1544. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1545. experimental = p;
  1546. } else
  1547. return p;
  1548. }
  1549. p = p->next;
  1550. }
  1551. return experimental;
  1552. }
  1553. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1554. {
  1555. AVCodec *p;
  1556. if (!name)
  1557. return NULL;
  1558. p = first_avcodec;
  1559. while (p) {
  1560. if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1561. return p;
  1562. p = p->next;
  1563. }
  1564. return NULL;
  1565. }
  1566. const char *avcodec_get_name(enum CodecID id)
  1567. {
  1568. AVCodec *codec;
  1569. #if !CONFIG_SMALL
  1570. switch (id) {
  1571. #include "libavcodec/codec_names.h"
  1572. }
  1573. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1574. #endif
  1575. codec = avcodec_find_decoder(id);
  1576. if (codec)
  1577. return codec->name;
  1578. codec = avcodec_find_encoder(id);
  1579. if (codec)
  1580. return codec->name;
  1581. return "unknown_codec";
  1582. }
  1583. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1584. {
  1585. int i, len, ret = 0;
  1586. #define IS_PRINT(x) \
  1587. (((x) >= '0' && (x) <= '9') || \
  1588. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1589. ((x) == '.' || (x) == ' '))
  1590. for (i = 0; i < 4; i++) {
  1591. len = snprintf(buf, buf_size,
  1592. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1593. buf += len;
  1594. buf_size = buf_size > len ? buf_size - len : 0;
  1595. ret += len;
  1596. codec_tag>>=8;
  1597. }
  1598. return ret;
  1599. }
  1600. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1601. {
  1602. const char *codec_type;
  1603. const char *codec_name;
  1604. const char *profile = NULL;
  1605. AVCodec *p;
  1606. int bitrate;
  1607. AVRational display_aspect_ratio;
  1608. if (!buf || buf_size <= 0)
  1609. return;
  1610. codec_type = av_get_media_type_string(enc->codec_type);
  1611. codec_name = avcodec_get_name(enc->codec_id);
  1612. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1613. if (enc->codec)
  1614. p = enc->codec;
  1615. else
  1616. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1617. avcodec_find_decoder(enc->codec_id);
  1618. if (p)
  1619. profile = av_get_profile_name(p, enc->profile);
  1620. }
  1621. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1622. codec_name, enc->mb_decision ? " (hq)" : "");
  1623. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1624. if (profile)
  1625. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1626. if (enc->codec_tag) {
  1627. char tag_buf[32];
  1628. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1629. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1630. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1631. }
  1632. switch(enc->codec_type) {
  1633. case AVMEDIA_TYPE_VIDEO:
  1634. if (enc->pix_fmt != PIX_FMT_NONE) {
  1635. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1636. ", %s",
  1637. av_get_pix_fmt_name(enc->pix_fmt));
  1638. }
  1639. if (enc->width) {
  1640. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1641. ", %dx%d",
  1642. enc->width, enc->height);
  1643. if (enc->sample_aspect_ratio.num) {
  1644. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1645. enc->width*enc->sample_aspect_ratio.num,
  1646. enc->height*enc->sample_aspect_ratio.den,
  1647. 1024*1024);
  1648. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1649. " [SAR %d:%d DAR %d:%d]",
  1650. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1651. display_aspect_ratio.num, display_aspect_ratio.den);
  1652. }
  1653. if(av_log_get_level() >= AV_LOG_DEBUG){
  1654. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1655. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1656. ", %d/%d",
  1657. enc->time_base.num/g, enc->time_base.den/g);
  1658. }
  1659. }
  1660. if (encode) {
  1661. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1662. ", q=%d-%d", enc->qmin, enc->qmax);
  1663. }
  1664. break;
  1665. case AVMEDIA_TYPE_AUDIO:
  1666. if (enc->sample_rate) {
  1667. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1668. ", %d Hz", enc->sample_rate);
  1669. }
  1670. av_strlcat(buf, ", ", buf_size);
  1671. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1672. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1673. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1674. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1675. }
  1676. break;
  1677. default:
  1678. return;
  1679. }
  1680. if (encode) {
  1681. if (enc->flags & CODEC_FLAG_PASS1)
  1682. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1683. ", pass 1");
  1684. if (enc->flags & CODEC_FLAG_PASS2)
  1685. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1686. ", pass 2");
  1687. }
  1688. bitrate = get_bit_rate(enc);
  1689. if (bitrate != 0) {
  1690. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1691. ", %d kb/s", bitrate / 1000);
  1692. }
  1693. }
  1694. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1695. {
  1696. const AVProfile *p;
  1697. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1698. return NULL;
  1699. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1700. if (p->profile == profile)
  1701. return p->name;
  1702. return NULL;
  1703. }
  1704. unsigned avcodec_version( void )
  1705. {
  1706. // av_assert0(CODEC_ID_V410==164);
  1707. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1708. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1709. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1710. av_assert0(CODEC_ID_SRT==94216);
  1711. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1712. return LIBAVCODEC_VERSION_INT;
  1713. }
  1714. const char *avcodec_configuration(void)
  1715. {
  1716. return FFMPEG_CONFIGURATION;
  1717. }
  1718. const char *avcodec_license(void)
  1719. {
  1720. #define LICENSE_PREFIX "libavcodec license: "
  1721. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1722. }
  1723. void avcodec_flush_buffers(AVCodecContext *avctx)
  1724. {
  1725. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1726. ff_thread_flush(avctx);
  1727. else if(avctx->codec->flush)
  1728. avctx->codec->flush(avctx);
  1729. avctx->pts_correction_last_pts =
  1730. avctx->pts_correction_last_dts = INT64_MIN;
  1731. }
  1732. static void video_free_buffers(AVCodecContext *s)
  1733. {
  1734. AVCodecInternal *avci = s->internal;
  1735. int i, j;
  1736. if (!avci->buffer)
  1737. return;
  1738. if (avci->buffer_count)
  1739. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1740. avci->buffer_count);
  1741. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1742. InternalBuffer *buf = &avci->buffer[i];
  1743. for(j=0; j<4; j++){
  1744. av_freep(&buf->base[j]);
  1745. buf->data[j]= NULL;
  1746. }
  1747. }
  1748. av_freep(&avci->buffer);
  1749. avci->buffer_count=0;
  1750. }
  1751. static void audio_free_buffers(AVCodecContext *avctx)
  1752. {
  1753. AVCodecInternal *avci = avctx->internal;
  1754. InternalBuffer *buf;
  1755. if (!avci->buffer)
  1756. return;
  1757. buf = avci->buffer;
  1758. if (buf->extended_data) {
  1759. av_free(buf->extended_data[0]);
  1760. if (buf->extended_data != buf->data)
  1761. av_freep(&buf->extended_data);
  1762. }
  1763. av_freep(&avci->buffer);
  1764. }
  1765. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1766. {
  1767. switch (avctx->codec_type) {
  1768. case AVMEDIA_TYPE_VIDEO:
  1769. video_free_buffers(avctx);
  1770. break;
  1771. case AVMEDIA_TYPE_AUDIO:
  1772. audio_free_buffers(avctx);
  1773. break;
  1774. default:
  1775. break;
  1776. }
  1777. }
  1778. int av_get_exact_bits_per_sample(enum CodecID codec_id)
  1779. {
  1780. switch(codec_id){
  1781. case CODEC_ID_ADPCM_CT:
  1782. case CODEC_ID_ADPCM_IMA_APC:
  1783. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1784. case CODEC_ID_ADPCM_IMA_WS:
  1785. case CODEC_ID_ADPCM_G722:
  1786. case CODEC_ID_ADPCM_YAMAHA:
  1787. return 4;
  1788. case CODEC_ID_PCM_ALAW:
  1789. case CODEC_ID_PCM_MULAW:
  1790. case CODEC_ID_PCM_S8:
  1791. case CODEC_ID_PCM_U8:
  1792. case CODEC_ID_PCM_ZORK:
  1793. return 8;
  1794. case CODEC_ID_PCM_S16BE:
  1795. case CODEC_ID_PCM_S16LE:
  1796. case CODEC_ID_PCM_S16LE_PLANAR:
  1797. case CODEC_ID_PCM_U16BE:
  1798. case CODEC_ID_PCM_U16LE:
  1799. return 16;
  1800. case CODEC_ID_PCM_S24DAUD:
  1801. case CODEC_ID_PCM_S24BE:
  1802. case CODEC_ID_PCM_S24LE:
  1803. case CODEC_ID_PCM_U24BE:
  1804. case CODEC_ID_PCM_U24LE:
  1805. return 24;
  1806. case CODEC_ID_PCM_S32BE:
  1807. case CODEC_ID_PCM_S32LE:
  1808. case CODEC_ID_PCM_U32BE:
  1809. case CODEC_ID_PCM_U32LE:
  1810. case CODEC_ID_PCM_F32BE:
  1811. case CODEC_ID_PCM_F32LE:
  1812. return 32;
  1813. case CODEC_ID_PCM_F64BE:
  1814. case CODEC_ID_PCM_F64LE:
  1815. return 64;
  1816. default:
  1817. return 0;
  1818. }
  1819. }
  1820. enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1821. {
  1822. static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
  1823. [AV_SAMPLE_FMT_U8 ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1824. [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1825. [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1826. [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1827. [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1828. [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8, CODEC_ID_PCM_U8 },
  1829. [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
  1830. [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
  1831. [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
  1832. [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
  1833. };
  1834. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1835. return CODEC_ID_NONE;
  1836. if (be < 0 || be > 1)
  1837. be = AV_NE(1, 0);
  1838. return map[fmt][be];
  1839. }
  1840. int av_get_bits_per_sample(enum CodecID codec_id)
  1841. {
  1842. switch (codec_id) {
  1843. case CODEC_ID_ADPCM_SBPRO_2:
  1844. return 2;
  1845. case CODEC_ID_ADPCM_SBPRO_3:
  1846. return 3;
  1847. case CODEC_ID_ADPCM_SBPRO_4:
  1848. case CODEC_ID_ADPCM_IMA_WAV:
  1849. case CODEC_ID_ADPCM_IMA_QT:
  1850. case CODEC_ID_ADPCM_SWF:
  1851. case CODEC_ID_ADPCM_MS:
  1852. return 4;
  1853. default:
  1854. return av_get_exact_bits_per_sample(codec_id);
  1855. }
  1856. }
  1857. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1858. {
  1859. int id, sr, ch, ba, tag, bps;
  1860. id = avctx->codec_id;
  1861. sr = avctx->sample_rate;
  1862. ch = avctx->channels;
  1863. ba = avctx->block_align;
  1864. tag = avctx->codec_tag;
  1865. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1866. /* codecs with an exact constant bits per sample */
  1867. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1868. return (frame_bytes * 8LL) / (bps * ch);
  1869. bps = avctx->bits_per_coded_sample;
  1870. /* codecs with a fixed packet duration */
  1871. switch (id) {
  1872. case CODEC_ID_ADPCM_ADX: return 32;
  1873. case CODEC_ID_ADPCM_IMA_QT: return 64;
  1874. case CODEC_ID_ADPCM_EA_XAS: return 128;
  1875. case CODEC_ID_AMR_NB:
  1876. case CODEC_ID_GSM:
  1877. case CODEC_ID_QCELP:
  1878. case CODEC_ID_RA_144:
  1879. case CODEC_ID_RA_288: return 160;
  1880. case CODEC_ID_IMC: return 256;
  1881. case CODEC_ID_AMR_WB:
  1882. case CODEC_ID_GSM_MS: return 320;
  1883. case CODEC_ID_MP1: return 384;
  1884. case CODEC_ID_ATRAC1: return 512;
  1885. case CODEC_ID_ATRAC3: return 1024;
  1886. case CODEC_ID_MP2:
  1887. case CODEC_ID_MUSEPACK7: return 1152;
  1888. case CODEC_ID_AC3: return 1536;
  1889. }
  1890. if (sr > 0) {
  1891. /* calc from sample rate */
  1892. if (id == CODEC_ID_TTA)
  1893. return 256 * sr / 245;
  1894. if (ch > 0) {
  1895. /* calc from sample rate and channels */
  1896. if (id == CODEC_ID_BINKAUDIO_DCT)
  1897. return (480 << (sr / 22050)) / ch;
  1898. }
  1899. }
  1900. if (ba > 0) {
  1901. /* calc from block_align */
  1902. if (id == CODEC_ID_SIPR) {
  1903. switch (ba) {
  1904. case 20: return 160;
  1905. case 19: return 144;
  1906. case 29: return 288;
  1907. case 37: return 480;
  1908. }
  1909. } else if (id == CODEC_ID_ILBC) {
  1910. switch (ba) {
  1911. case 38: return 160;
  1912. case 50: return 240;
  1913. }
  1914. }
  1915. }
  1916. if (frame_bytes > 0) {
  1917. /* calc from frame_bytes only */
  1918. if (id == CODEC_ID_TRUESPEECH)
  1919. return 240 * (frame_bytes / 32);
  1920. if (id == CODEC_ID_NELLYMOSER)
  1921. return 256 * (frame_bytes / 64);
  1922. if (bps > 0) {
  1923. /* calc from frame_bytes and bits_per_coded_sample */
  1924. if (id == CODEC_ID_ADPCM_G726)
  1925. return frame_bytes * 8 / bps;
  1926. }
  1927. if (ch > 0) {
  1928. /* calc from frame_bytes and channels */
  1929. switch (id) {
  1930. case CODEC_ID_ADPCM_4XM:
  1931. case CODEC_ID_ADPCM_IMA_ISS:
  1932. return (frame_bytes - 4 * ch) * 2 / ch;
  1933. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1934. return (frame_bytes - 4) * 2 / ch;
  1935. case CODEC_ID_ADPCM_IMA_AMV:
  1936. return (frame_bytes - 8) * 2 / ch;
  1937. case CODEC_ID_ADPCM_XA:
  1938. return (frame_bytes / 128) * 224 / ch;
  1939. case CODEC_ID_INTERPLAY_DPCM:
  1940. return (frame_bytes - 6 - ch) / ch;
  1941. case CODEC_ID_ROQ_DPCM:
  1942. return (frame_bytes - 8) / ch;
  1943. case CODEC_ID_XAN_DPCM:
  1944. return (frame_bytes - 2 * ch) / ch;
  1945. case CODEC_ID_MACE3:
  1946. return 3 * frame_bytes / ch;
  1947. case CODEC_ID_MACE6:
  1948. return 6 * frame_bytes / ch;
  1949. case CODEC_ID_PCM_LXF:
  1950. return 2 * (frame_bytes / (5 * ch));
  1951. }
  1952. if (tag) {
  1953. /* calc from frame_bytes, channels, and codec_tag */
  1954. if (id == CODEC_ID_SOL_DPCM) {
  1955. if (tag == 3)
  1956. return frame_bytes / ch;
  1957. else
  1958. return frame_bytes * 2 / ch;
  1959. }
  1960. }
  1961. if (ba > 0) {
  1962. /* calc from frame_bytes, channels, and block_align */
  1963. int blocks = frame_bytes / ba;
  1964. switch (avctx->codec_id) {
  1965. case CODEC_ID_ADPCM_IMA_WAV:
  1966. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1967. case CODEC_ID_ADPCM_IMA_DK3:
  1968. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1969. case CODEC_ID_ADPCM_IMA_DK4:
  1970. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1971. case CODEC_ID_ADPCM_MS:
  1972. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1973. }
  1974. }
  1975. if (bps > 0) {
  1976. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1977. switch (avctx->codec_id) {
  1978. case CODEC_ID_PCM_DVD:
  1979. if(bps<4)
  1980. return 0;
  1981. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1982. case CODEC_ID_PCM_BLURAY:
  1983. if(bps<4)
  1984. return 0;
  1985. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1986. case CODEC_ID_S302M:
  1987. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1988. }
  1989. }
  1990. }
  1991. }
  1992. return 0;
  1993. }
  1994. #if !HAVE_THREADS
  1995. int ff_thread_init(AVCodecContext *s){
  1996. return -1;
  1997. }
  1998. #endif
  1999. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2000. {
  2001. unsigned int n = 0;
  2002. while(v >= 0xff) {
  2003. *s++ = 0xff;
  2004. v -= 0xff;
  2005. n++;
  2006. }
  2007. *s = v;
  2008. n++;
  2009. return n;
  2010. }
  2011. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  2012. int i;
  2013. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  2014. return i;
  2015. }
  2016. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2017. {
  2018. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  2019. "version to the newest one from Git. If the problem still "
  2020. "occurs, it means that your file has a feature which has not "
  2021. "been implemented.\n", feature);
  2022. if(want_sample)
  2023. av_log_ask_for_sample(avc, NULL);
  2024. }
  2025. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2026. {
  2027. va_list argument_list;
  2028. va_start(argument_list, msg);
  2029. if (msg)
  2030. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2031. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2032. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2033. "and contact the ffmpeg-devel mailing list.\n");
  2034. va_end(argument_list);
  2035. }
  2036. static AVHWAccel *first_hwaccel = NULL;
  2037. void av_register_hwaccel(AVHWAccel *hwaccel)
  2038. {
  2039. AVHWAccel **p = &first_hwaccel;
  2040. while (*p)
  2041. p = &(*p)->next;
  2042. *p = hwaccel;
  2043. hwaccel->next = NULL;
  2044. }
  2045. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2046. {
  2047. return hwaccel ? hwaccel->next : first_hwaccel;
  2048. }
  2049. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  2050. {
  2051. AVHWAccel *hwaccel=NULL;
  2052. while((hwaccel= av_hwaccel_next(hwaccel))){
  2053. if ( hwaccel->id == codec_id
  2054. && hwaccel->pix_fmt == pix_fmt)
  2055. return hwaccel;
  2056. }
  2057. return NULL;
  2058. }
  2059. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2060. {
  2061. if (ff_lockmgr_cb) {
  2062. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2063. return -1;
  2064. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2065. return -1;
  2066. }
  2067. ff_lockmgr_cb = cb;
  2068. if (ff_lockmgr_cb) {
  2069. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2070. return -1;
  2071. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2072. return -1;
  2073. }
  2074. return 0;
  2075. }
  2076. int avpriv_lock_avformat(void)
  2077. {
  2078. if (ff_lockmgr_cb) {
  2079. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2080. return -1;
  2081. }
  2082. return 0;
  2083. }
  2084. int avpriv_unlock_avformat(void)
  2085. {
  2086. if (ff_lockmgr_cb) {
  2087. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2088. return -1;
  2089. }
  2090. return 0;
  2091. }
  2092. unsigned int avpriv_toupper4(unsigned int x)
  2093. {
  2094. return toupper( x &0xFF)
  2095. + (toupper((x>>8 )&0xFF)<<8 )
  2096. + (toupper((x>>16)&0xFF)<<16)
  2097. + (toupper((x>>24)&0xFF)<<24);
  2098. }
  2099. #if !HAVE_THREADS
  2100. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2101. {
  2102. f->owner = avctx;
  2103. ff_init_buffer_info(avctx, f);
  2104. return avctx->get_buffer(avctx, f);
  2105. }
  2106. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2107. {
  2108. f->owner->release_buffer(f->owner, f);
  2109. }
  2110. void ff_thread_finish_setup(AVCodecContext *avctx)
  2111. {
  2112. }
  2113. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2114. {
  2115. }
  2116. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2117. {
  2118. }
  2119. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2120. {
  2121. return 1;
  2122. }
  2123. #endif
  2124. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  2125. {
  2126. AVCodec *c= avcodec_find_decoder(codec_id);
  2127. if(!c)
  2128. c= avcodec_find_encoder(codec_id);
  2129. if(c)
  2130. return c->type;
  2131. if (codec_id <= CODEC_ID_NONE)
  2132. return AVMEDIA_TYPE_UNKNOWN;
  2133. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  2134. return AVMEDIA_TYPE_VIDEO;
  2135. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  2136. return AVMEDIA_TYPE_AUDIO;
  2137. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  2138. return AVMEDIA_TYPE_SUBTITLE;
  2139. return AVMEDIA_TYPE_UNKNOWN;
  2140. }
  2141. int avcodec_is_open(AVCodecContext *s)
  2142. {
  2143. return !!s->internal;
  2144. }