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.

3134 lines
101KB

  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 "config.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/bprint.h"
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/crc.h"
  32. #include "libavutil/frame.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/pixdesc.h"
  35. #include "libavutil/imgutils.h"
  36. #include "libavutil/samplefmt.h"
  37. #include "libavutil/dict.h"
  38. #include "libavutil/avassert.h"
  39. #include "avcodec.h"
  40. #include "dsputil.h"
  41. #include "libavutil/opt.h"
  42. #include "thread.h"
  43. #include "frame_thread_encoder.h"
  44. #include "internal.h"
  45. #include "bytestream.h"
  46. #include "version.h"
  47. #include <stdlib.h>
  48. #include <stdarg.h>
  49. #include <limits.h>
  50. #include <float.h>
  51. #if CONFIG_ICONV
  52. # include <iconv.h>
  53. #endif
  54. volatile int ff_avcodec_locked;
  55. static int volatile entangled_thread_counter = 0;
  56. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  57. static void *codec_mutex;
  58. static void *avformat_mutex;
  59. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  60. {
  61. if (min_size < *size)
  62. return ptr;
  63. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  64. ptr = av_realloc(ptr, min_size);
  65. /* we could set this to the unmodified min_size but this is safer
  66. * if the user lost the ptr and uses NULL now
  67. */
  68. if (!ptr)
  69. min_size = 0;
  70. *size = min_size;
  71. return ptr;
  72. }
  73. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  74. {
  75. void **p = ptr;
  76. if (min_size < *size)
  77. return 0;
  78. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  79. av_free(*p);
  80. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  81. if (!*p)
  82. min_size = 0;
  83. *size = min_size;
  84. return 1;
  85. }
  86. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  87. {
  88. ff_fast_malloc(ptr, size, min_size, 0);
  89. }
  90. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  91. {
  92. uint8_t **p = ptr;
  93. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  94. av_freep(p);
  95. *size = 0;
  96. return;
  97. }
  98. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  99. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  100. }
  101. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  102. {
  103. uint8_t **p = ptr;
  104. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  105. av_freep(p);
  106. *size = 0;
  107. return;
  108. }
  109. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  110. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  111. }
  112. /* encoder management */
  113. static AVCodec *first_avcodec = NULL;
  114. AVCodec *av_codec_next(const AVCodec *c)
  115. {
  116. if (c)
  117. return c->next;
  118. else
  119. return first_avcodec;
  120. }
  121. static void avcodec_init(void)
  122. {
  123. static int initialized = 0;
  124. if (initialized != 0)
  125. return;
  126. initialized = 1;
  127. if (CONFIG_DSPUTIL)
  128. ff_dsputil_static_init();
  129. }
  130. int av_codec_is_encoder(const AVCodec *codec)
  131. {
  132. return codec && (codec->encode_sub || codec->encode2);
  133. }
  134. int av_codec_is_decoder(const AVCodec *codec)
  135. {
  136. return codec && codec->decode;
  137. }
  138. void avcodec_register(AVCodec *codec)
  139. {
  140. AVCodec **p;
  141. avcodec_init();
  142. p = &first_avcodec;
  143. while (*p != NULL)
  144. p = &(*p)->next;
  145. *p = codec;
  146. codec->next = NULL;
  147. if (codec->init_static_data)
  148. codec->init_static_data(codec);
  149. }
  150. unsigned avcodec_get_edge_width(void)
  151. {
  152. return EDGE_WIDTH;
  153. }
  154. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  155. {
  156. s->coded_width = width;
  157. s->coded_height = height;
  158. s->width = -((-width ) >> s->lowres);
  159. s->height = -((-height) >> s->lowres);
  160. }
  161. #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
  162. # define STRIDE_ALIGN 16
  163. #else
  164. # define STRIDE_ALIGN 8
  165. #endif
  166. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  167. int linesize_align[AV_NUM_DATA_POINTERS])
  168. {
  169. int i;
  170. int w_align = 1;
  171. int h_align = 1;
  172. switch (s->pix_fmt) {
  173. case AV_PIX_FMT_YUV420P:
  174. case AV_PIX_FMT_YUYV422:
  175. case AV_PIX_FMT_UYVY422:
  176. case AV_PIX_FMT_YUV422P:
  177. case AV_PIX_FMT_YUV440P:
  178. case AV_PIX_FMT_YUV444P:
  179. case AV_PIX_FMT_GBRP:
  180. case AV_PIX_FMT_GRAY8:
  181. case AV_PIX_FMT_GRAY16BE:
  182. case AV_PIX_FMT_GRAY16LE:
  183. case AV_PIX_FMT_YUVJ420P:
  184. case AV_PIX_FMT_YUVJ422P:
  185. case AV_PIX_FMT_YUVJ440P:
  186. case AV_PIX_FMT_YUVJ444P:
  187. case AV_PIX_FMT_YUVA420P:
  188. case AV_PIX_FMT_YUVA422P:
  189. case AV_PIX_FMT_YUVA444P:
  190. case AV_PIX_FMT_YUV420P9LE:
  191. case AV_PIX_FMT_YUV420P9BE:
  192. case AV_PIX_FMT_YUV420P10LE:
  193. case AV_PIX_FMT_YUV420P10BE:
  194. case AV_PIX_FMT_YUV420P12LE:
  195. case AV_PIX_FMT_YUV420P12BE:
  196. case AV_PIX_FMT_YUV420P14LE:
  197. case AV_PIX_FMT_YUV420P14BE:
  198. case AV_PIX_FMT_YUV422P9LE:
  199. case AV_PIX_FMT_YUV422P9BE:
  200. case AV_PIX_FMT_YUV422P10LE:
  201. case AV_PIX_FMT_YUV422P10BE:
  202. case AV_PIX_FMT_YUV422P12LE:
  203. case AV_PIX_FMT_YUV422P12BE:
  204. case AV_PIX_FMT_YUV422P14LE:
  205. case AV_PIX_FMT_YUV422P14BE:
  206. case AV_PIX_FMT_YUV444P9LE:
  207. case AV_PIX_FMT_YUV444P9BE:
  208. case AV_PIX_FMT_YUV444P10LE:
  209. case AV_PIX_FMT_YUV444P10BE:
  210. case AV_PIX_FMT_YUV444P12LE:
  211. case AV_PIX_FMT_YUV444P12BE:
  212. case AV_PIX_FMT_YUV444P14LE:
  213. case AV_PIX_FMT_YUV444P14BE:
  214. case AV_PIX_FMT_GBRP9LE:
  215. case AV_PIX_FMT_GBRP9BE:
  216. case AV_PIX_FMT_GBRP10LE:
  217. case AV_PIX_FMT_GBRP10BE:
  218. case AV_PIX_FMT_GBRP12LE:
  219. case AV_PIX_FMT_GBRP12BE:
  220. case AV_PIX_FMT_GBRP14LE:
  221. case AV_PIX_FMT_GBRP14BE:
  222. w_align = 16; //FIXME assume 16 pixel per macroblock
  223. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  224. break;
  225. case AV_PIX_FMT_YUV411P:
  226. case AV_PIX_FMT_UYYVYY411:
  227. w_align = 32;
  228. h_align = 8;
  229. break;
  230. case AV_PIX_FMT_YUV410P:
  231. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  232. w_align = 64;
  233. h_align = 64;
  234. }
  235. break;
  236. case AV_PIX_FMT_RGB555:
  237. if (s->codec_id == AV_CODEC_ID_RPZA) {
  238. w_align = 4;
  239. h_align = 4;
  240. }
  241. break;
  242. case AV_PIX_FMT_PAL8:
  243. case AV_PIX_FMT_BGR8:
  244. case AV_PIX_FMT_RGB8:
  245. if (s->codec_id == AV_CODEC_ID_SMC ||
  246. s->codec_id == AV_CODEC_ID_CINEPAK) {
  247. w_align = 4;
  248. h_align = 4;
  249. }
  250. break;
  251. case AV_PIX_FMT_BGR24:
  252. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  253. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  254. w_align = 4;
  255. h_align = 4;
  256. }
  257. break;
  258. case AV_PIX_FMT_RGB24:
  259. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  260. w_align = 4;
  261. h_align = 4;
  262. }
  263. break;
  264. default:
  265. w_align = 1;
  266. h_align = 1;
  267. break;
  268. }
  269. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  270. w_align = FFMAX(w_align, 8);
  271. }
  272. *width = FFALIGN(*width, w_align);
  273. *height = FFALIGN(*height, h_align);
  274. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  275. // some of the optimized chroma MC reads one line too much
  276. // which is also done in mpeg decoders with lowres > 0
  277. *height += 2;
  278. for (i = 0; i < 4; i++)
  279. linesize_align[i] = STRIDE_ALIGN;
  280. }
  281. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  282. {
  283. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  284. int chroma_shift = desc->log2_chroma_w;
  285. int linesize_align[AV_NUM_DATA_POINTERS];
  286. int align;
  287. avcodec_align_dimensions2(s, width, height, linesize_align);
  288. align = FFMAX(linesize_align[0], linesize_align[3]);
  289. linesize_align[1] <<= chroma_shift;
  290. linesize_align[2] <<= chroma_shift;
  291. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  292. *width = FFALIGN(*width, align);
  293. }
  294. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  295. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  296. int buf_size, int align)
  297. {
  298. int ch, planar, needed_size, ret = 0;
  299. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  300. frame->nb_samples, sample_fmt,
  301. align);
  302. if (buf_size < needed_size)
  303. return AVERROR(EINVAL);
  304. planar = av_sample_fmt_is_planar(sample_fmt);
  305. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  306. if (!(frame->extended_data = av_mallocz(nb_channels *
  307. sizeof(*frame->extended_data))))
  308. return AVERROR(ENOMEM);
  309. } else {
  310. frame->extended_data = frame->data;
  311. }
  312. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  313. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  314. sample_fmt, align)) < 0) {
  315. if (frame->extended_data != frame->data)
  316. av_freep(&frame->extended_data);
  317. return ret;
  318. }
  319. if (frame->extended_data != frame->data) {
  320. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  321. frame->data[ch] = frame->extended_data[ch];
  322. }
  323. return ret;
  324. }
  325. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  326. {
  327. FramePool *pool = avctx->internal->pool;
  328. int i, ret;
  329. switch (avctx->codec_type) {
  330. case AVMEDIA_TYPE_VIDEO: {
  331. AVPicture picture;
  332. int size[4] = { 0 };
  333. int w = frame->width;
  334. int h = frame->height;
  335. int tmpsize, unaligned;
  336. if (pool->format == frame->format &&
  337. pool->width == frame->width && pool->height == frame->height)
  338. return 0;
  339. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  340. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  341. w += EDGE_WIDTH * 2;
  342. h += EDGE_WIDTH * 2;
  343. }
  344. do {
  345. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  346. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  347. av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  348. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  349. w += w & ~(w - 1);
  350. unaligned = 0;
  351. for (i = 0; i < 4; i++)
  352. unaligned |= picture.linesize[i] % pool->stride_align[i];
  353. } while (unaligned);
  354. tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  355. NULL, picture.linesize);
  356. if (tmpsize < 0)
  357. return -1;
  358. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  359. size[i] = picture.data[i + 1] - picture.data[i];
  360. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  361. for (i = 0; i < 4; i++) {
  362. av_buffer_pool_uninit(&pool->pools[i]);
  363. pool->linesize[i] = picture.linesize[i];
  364. if (size[i]) {
  365. pool->pools[i] = av_buffer_pool_init(size[i] + 16,
  366. CONFIG_MEMORY_POISONING ?
  367. NULL :
  368. av_buffer_allocz);
  369. if (!pool->pools[i]) {
  370. ret = AVERROR(ENOMEM);
  371. goto fail;
  372. }
  373. }
  374. }
  375. pool->format = frame->format;
  376. pool->width = frame->width;
  377. pool->height = frame->height;
  378. break;
  379. }
  380. case AVMEDIA_TYPE_AUDIO: {
  381. int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
  382. int planar = av_sample_fmt_is_planar(frame->format);
  383. int planes = planar ? ch : 1;
  384. if (pool->format == frame->format && pool->planes == planes &&
  385. pool->channels == ch && frame->nb_samples == pool->samples)
  386. return 0;
  387. av_buffer_pool_uninit(&pool->pools[0]);
  388. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  389. frame->nb_samples, frame->format, 0);
  390. if (ret < 0)
  391. goto fail;
  392. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  393. if (!pool->pools[0]) {
  394. ret = AVERROR(ENOMEM);
  395. goto fail;
  396. }
  397. pool->format = frame->format;
  398. pool->planes = planes;
  399. pool->channels = ch;
  400. pool->samples = frame->nb_samples;
  401. break;
  402. }
  403. default: av_assert0(0);
  404. }
  405. return 0;
  406. fail:
  407. for (i = 0; i < 4; i++)
  408. av_buffer_pool_uninit(&pool->pools[i]);
  409. pool->format = -1;
  410. pool->planes = pool->channels = pool->samples = 0;
  411. pool->width = pool->height = 0;
  412. return ret;
  413. }
  414. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  415. {
  416. FramePool *pool = avctx->internal->pool;
  417. int planes = pool->planes;
  418. int i;
  419. frame->linesize[0] = pool->linesize[0];
  420. if (planes > AV_NUM_DATA_POINTERS) {
  421. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  422. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  423. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  424. sizeof(*frame->extended_buf));
  425. if (!frame->extended_data || !frame->extended_buf) {
  426. av_freep(&frame->extended_data);
  427. av_freep(&frame->extended_buf);
  428. return AVERROR(ENOMEM);
  429. }
  430. } else {
  431. frame->extended_data = frame->data;
  432. av_assert0(frame->nb_extended_buf == 0);
  433. }
  434. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  435. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  436. if (!frame->buf[i])
  437. goto fail;
  438. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  439. }
  440. for (i = 0; i < frame->nb_extended_buf; i++) {
  441. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  442. if (!frame->extended_buf[i])
  443. goto fail;
  444. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  445. }
  446. if (avctx->debug & FF_DEBUG_BUFFERS)
  447. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  448. return 0;
  449. fail:
  450. av_frame_unref(frame);
  451. return AVERROR(ENOMEM);
  452. }
  453. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  454. {
  455. FramePool *pool = s->internal->pool;
  456. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  457. int pixel_size = desc->comp[0].step_minus1 + 1;
  458. int h_chroma_shift, v_chroma_shift;
  459. int i;
  460. if (pic->data[0] != NULL) {
  461. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  462. return -1;
  463. }
  464. memset(pic->data, 0, sizeof(pic->data));
  465. pic->extended_data = pic->data;
  466. av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  467. for (i = 0; i < 4 && pool->pools[i]; i++) {
  468. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  469. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  470. pic->linesize[i] = pool->linesize[i];
  471. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  472. if (!pic->buf[i])
  473. goto fail;
  474. // no edge if EDGE EMU or not planar YUV
  475. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
  476. pic->data[i] = pic->buf[i]->data;
  477. else {
  478. pic->data[i] = pic->buf[i]->data +
  479. FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
  480. (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
  481. }
  482. }
  483. for (; i < AV_NUM_DATA_POINTERS; i++) {
  484. pic->data[i] = NULL;
  485. pic->linesize[i] = 0;
  486. }
  487. if (pic->data[1] && !pic->data[2])
  488. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  489. if (s->debug & FF_DEBUG_BUFFERS)
  490. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  491. return 0;
  492. fail:
  493. av_frame_unref(pic);
  494. return AVERROR(ENOMEM);
  495. }
  496. void avpriv_color_frame(AVFrame *frame, const int c[4])
  497. {
  498. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  499. int p, y, x;
  500. av_assert0(desc->flags & PIX_FMT_PLANAR);
  501. for (p = 0; p<desc->nb_components; p++) {
  502. uint8_t *dst = frame->data[p];
  503. int is_chroma = p == 1 || p == 2;
  504. int bytes = -((-frame->width) >> (is_chroma ? desc->log2_chroma_w : 0));
  505. for (y = 0; y<-((-frame->height) >> (is_chroma ? desc->log2_chroma_h : 0)); y++){
  506. if (desc->comp[0].depth_minus1 >= 8) {
  507. for (x = 0; x<bytes; x++)
  508. ((uint16_t*)dst)[x] = c[p];
  509. }else
  510. memset(dst, c[p], bytes);
  511. dst += frame->linesize[p];
  512. }
  513. }
  514. }
  515. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  516. {
  517. int ret;
  518. if ((ret = update_frame_pool(avctx, frame)) < 0)
  519. return ret;
  520. #if FF_API_GET_BUFFER
  521. frame->type = FF_BUFFER_TYPE_INTERNAL;
  522. #endif
  523. switch (avctx->codec_type) {
  524. case AVMEDIA_TYPE_VIDEO:
  525. return video_get_buffer(avctx, frame);
  526. case AVMEDIA_TYPE_AUDIO:
  527. return audio_get_buffer(avctx, frame);
  528. default:
  529. return -1;
  530. }
  531. }
  532. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  533. {
  534. if (avctx->pkt) {
  535. frame->pkt_pts = avctx->pkt->pts;
  536. av_frame_set_pkt_pos (frame, avctx->pkt->pos);
  537. av_frame_set_pkt_duration(frame, avctx->pkt->duration);
  538. av_frame_set_pkt_size (frame, avctx->pkt->size);
  539. } else {
  540. frame->pkt_pts = AV_NOPTS_VALUE;
  541. av_frame_set_pkt_pos (frame, -1);
  542. av_frame_set_pkt_duration(frame, 0);
  543. av_frame_set_pkt_size (frame, -1);
  544. }
  545. frame->reordered_opaque = avctx->reordered_opaque;
  546. switch (avctx->codec->type) {
  547. case AVMEDIA_TYPE_VIDEO:
  548. if (!frame->width)
  549. frame->width = avctx->width;
  550. if (!frame->height)
  551. frame->height = avctx->height;
  552. if (frame->format < 0)
  553. frame->format = avctx->pix_fmt;
  554. if (!frame->sample_aspect_ratio.num)
  555. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  556. break;
  557. case AVMEDIA_TYPE_AUDIO:
  558. if (!frame->sample_rate)
  559. frame->sample_rate = avctx->sample_rate;
  560. if (frame->format < 0)
  561. frame->format = avctx->sample_fmt;
  562. if (!frame->channel_layout) {
  563. if (avctx->channel_layout) {
  564. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  565. avctx->channels) {
  566. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  567. "configuration.\n");
  568. return AVERROR(EINVAL);
  569. }
  570. frame->channel_layout = avctx->channel_layout;
  571. } else {
  572. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  573. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  574. avctx->channels);
  575. return AVERROR(ENOSYS);
  576. }
  577. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  578. }
  579. }
  580. av_frame_set_channels(frame, avctx->channels);
  581. break;
  582. }
  583. return 0;
  584. }
  585. #if FF_API_GET_BUFFER
  586. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  587. {
  588. return avcodec_default_get_buffer2(avctx, frame, 0);
  589. }
  590. typedef struct CompatReleaseBufPriv {
  591. AVCodecContext avctx;
  592. AVFrame frame;
  593. } CompatReleaseBufPriv;
  594. static void compat_free_buffer(void *opaque, uint8_t *data)
  595. {
  596. CompatReleaseBufPriv *priv = opaque;
  597. if (priv->avctx.release_buffer)
  598. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  599. av_freep(&priv);
  600. }
  601. static void compat_release_buffer(void *opaque, uint8_t *data)
  602. {
  603. AVBufferRef *buf = opaque;
  604. av_buffer_unref(&buf);
  605. }
  606. #endif
  607. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  608. {
  609. int ret;
  610. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  611. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  612. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  613. return AVERROR(EINVAL);
  614. }
  615. }
  616. if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
  617. return ret;
  618. #if FF_API_GET_BUFFER
  619. /*
  620. * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
  621. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  622. * a dummy AVBuffer as its private data, unreffing it on free.
  623. * When all the planes are freed, the dummy buffer's free callback calls
  624. * release_buffer().
  625. */
  626. if (avctx->get_buffer) {
  627. CompatReleaseBufPriv *priv = NULL;
  628. AVBufferRef *dummy_buf = NULL;
  629. int planes, i, ret;
  630. if (flags & AV_GET_BUFFER_FLAG_REF)
  631. frame->reference = 1;
  632. ret = avctx->get_buffer(avctx, frame);
  633. if (ret < 0)
  634. return ret;
  635. /* return if the buffers are already set up
  636. * this would happen e.g. when a custom get_buffer() calls
  637. * avcodec_default_get_buffer
  638. */
  639. if (frame->buf[0])
  640. return 0;
  641. priv = av_mallocz(sizeof(*priv));
  642. if (!priv) {
  643. ret = AVERROR(ENOMEM);
  644. goto fail;
  645. }
  646. priv->avctx = *avctx;
  647. priv->frame = *frame;
  648. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  649. if (!dummy_buf) {
  650. ret = AVERROR(ENOMEM);
  651. goto fail;
  652. }
  653. #define WRAP_PLANE(ref_out, data, data_size) \
  654. do { \
  655. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  656. if (!dummy_ref) { \
  657. ret = AVERROR(ENOMEM); \
  658. goto fail; \
  659. } \
  660. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  661. dummy_ref, 0); \
  662. if (!ref_out) { \
  663. av_frame_unref(frame); \
  664. ret = AVERROR(ENOMEM); \
  665. goto fail; \
  666. } \
  667. } while (0)
  668. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  669. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  670. planes = av_pix_fmt_count_planes(frame->format);
  671. if (!planes)
  672. planes = 1;
  673. if (!desc || planes <= 0) {
  674. ret = AVERROR(EINVAL);
  675. goto fail;
  676. }
  677. for (i = 0; i < planes; i++) {
  678. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  679. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  680. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  681. }
  682. } else {
  683. int planar = av_sample_fmt_is_planar(frame->format);
  684. planes = planar ? avctx->channels : 1;
  685. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  686. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  687. frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
  688. frame->nb_extended_buf);
  689. if (!frame->extended_buf) {
  690. ret = AVERROR(ENOMEM);
  691. goto fail;
  692. }
  693. }
  694. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  695. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  696. for (i = 0; i < frame->nb_extended_buf; i++)
  697. WRAP_PLANE(frame->extended_buf[i],
  698. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  699. frame->linesize[0]);
  700. }
  701. av_buffer_unref(&dummy_buf);
  702. return 0;
  703. fail:
  704. avctx->release_buffer(avctx, frame);
  705. av_freep(&priv);
  706. av_buffer_unref(&dummy_buf);
  707. return ret;
  708. }
  709. #endif
  710. return avctx->get_buffer2(avctx, frame, flags);
  711. }
  712. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  713. {
  714. int ret = get_buffer_internal(avctx, frame, flags);
  715. if (ret < 0)
  716. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  717. return ret;
  718. }
  719. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  720. {
  721. AVFrame tmp;
  722. int ret;
  723. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  724. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  725. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  726. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  727. av_frame_unref(frame);
  728. }
  729. ff_init_buffer_info(avctx, frame);
  730. if (!frame->data[0])
  731. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  732. if (av_frame_is_writable(frame))
  733. return 0;
  734. av_frame_move_ref(&tmp, frame);
  735. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  736. if (ret < 0) {
  737. av_frame_unref(&tmp);
  738. return ret;
  739. }
  740. av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
  741. frame->format, frame->width, frame->height);
  742. av_frame_unref(&tmp);
  743. return 0;
  744. }
  745. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  746. {
  747. int ret = reget_buffer_internal(avctx, frame);
  748. if (ret < 0)
  749. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  750. return ret;
  751. }
  752. #if FF_API_GET_BUFFER
  753. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  754. {
  755. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  756. av_frame_unref(pic);
  757. }
  758. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  759. {
  760. av_assert0(0);
  761. }
  762. #endif
  763. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  764. {
  765. int i;
  766. for (i = 0; i < count; i++) {
  767. int r = func(c, (char *)arg + i * size);
  768. if (ret)
  769. ret[i] = r;
  770. }
  771. return 0;
  772. }
  773. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  774. {
  775. int i;
  776. for (i = 0; i < count; i++) {
  777. int r = func(c, arg, i, 0);
  778. if (ret)
  779. ret[i] = r;
  780. }
  781. return 0;
  782. }
  783. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  784. {
  785. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  786. return desc->flags & PIX_FMT_HWACCEL;
  787. }
  788. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  789. {
  790. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  791. ++fmt;
  792. return fmt[0];
  793. }
  794. void avcodec_get_frame_defaults(AVFrame *frame)
  795. {
  796. #if LIBAVCODEC_VERSION_MAJOR >= 55
  797. // extended_data should explicitly be freed when needed, this code is unsafe currently
  798. // also this is not compatible to the <55 ABI/API
  799. if (frame->extended_data != frame->data && 0)
  800. av_freep(&frame->extended_data);
  801. #endif
  802. memset(frame, 0, sizeof(AVFrame));
  803. frame->pts =
  804. frame->pkt_dts =
  805. frame->pkt_pts = AV_NOPTS_VALUE;
  806. av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
  807. av_frame_set_pkt_duration (frame, 0);
  808. av_frame_set_pkt_pos (frame, -1);
  809. av_frame_set_pkt_size (frame, -1);
  810. frame->key_frame = 1;
  811. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  812. frame->format = -1; /* unknown */
  813. frame->extended_data = frame->data;
  814. }
  815. AVFrame *avcodec_alloc_frame(void)
  816. {
  817. AVFrame *frame = av_malloc(sizeof(AVFrame));
  818. if (frame == NULL)
  819. return NULL;
  820. frame->extended_data = NULL;
  821. avcodec_get_frame_defaults(frame);
  822. return frame;
  823. }
  824. void avcodec_free_frame(AVFrame **frame)
  825. {
  826. AVFrame *f;
  827. if (!frame || !*frame)
  828. return;
  829. f = *frame;
  830. if (f->extended_data != f->data)
  831. av_freep(&f->extended_data);
  832. av_freep(frame);
  833. }
  834. #define MAKE_ACCESSORS(str, name, type, field) \
  835. type av_##name##_get_##field(const str *s) { return s->field; } \
  836. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  837. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  838. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  839. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  840. {
  841. memset(sub, 0, sizeof(*sub));
  842. sub->pts = AV_NOPTS_VALUE;
  843. }
  844. static int get_bit_rate(AVCodecContext *ctx)
  845. {
  846. int bit_rate;
  847. int bits_per_sample;
  848. switch (ctx->codec_type) {
  849. case AVMEDIA_TYPE_VIDEO:
  850. case AVMEDIA_TYPE_DATA:
  851. case AVMEDIA_TYPE_SUBTITLE:
  852. case AVMEDIA_TYPE_ATTACHMENT:
  853. bit_rate = ctx->bit_rate;
  854. break;
  855. case AVMEDIA_TYPE_AUDIO:
  856. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  857. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  858. break;
  859. default:
  860. bit_rate = 0;
  861. break;
  862. }
  863. return bit_rate;
  864. }
  865. #if FF_API_AVCODEC_OPEN
  866. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  867. {
  868. return avcodec_open2(avctx, codec, NULL);
  869. }
  870. #endif
  871. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  872. {
  873. int ret = 0;
  874. ff_unlock_avcodec();
  875. ret = avcodec_open2(avctx, codec, options);
  876. ff_lock_avcodec(avctx);
  877. return ret;
  878. }
  879. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  880. {
  881. int ret = 0;
  882. AVDictionary *tmp = NULL;
  883. if (avcodec_is_open(avctx))
  884. return 0;
  885. if ((!codec && !avctx->codec)) {
  886. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  887. return AVERROR(EINVAL);
  888. }
  889. if ((codec && avctx->codec && codec != avctx->codec)) {
  890. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  891. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  892. return AVERROR(EINVAL);
  893. }
  894. if (!codec)
  895. codec = avctx->codec;
  896. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  897. return AVERROR(EINVAL);
  898. if (options)
  899. av_dict_copy(&tmp, *options, 0);
  900. ret = ff_lock_avcodec(avctx);
  901. if (ret < 0)
  902. return ret;
  903. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  904. if (!avctx->internal) {
  905. ret = AVERROR(ENOMEM);
  906. goto end;
  907. }
  908. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  909. if (!avctx->internal->pool) {
  910. ret = AVERROR(ENOMEM);
  911. goto free_and_end;
  912. }
  913. if (codec->priv_data_size > 0) {
  914. if (!avctx->priv_data) {
  915. avctx->priv_data = av_mallocz(codec->priv_data_size);
  916. if (!avctx->priv_data) {
  917. ret = AVERROR(ENOMEM);
  918. goto end;
  919. }
  920. if (codec->priv_class) {
  921. *(const AVClass **)avctx->priv_data = codec->priv_class;
  922. av_opt_set_defaults(avctx->priv_data);
  923. }
  924. }
  925. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  926. goto free_and_end;
  927. } else {
  928. avctx->priv_data = NULL;
  929. }
  930. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  931. goto free_and_end;
  932. // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
  933. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  934. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
  935. if (avctx->coded_width && avctx->coded_height)
  936. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  937. else if (avctx->width && avctx->height)
  938. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  939. }
  940. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  941. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  942. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  943. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  944. avcodec_set_dimensions(avctx, 0, 0);
  945. }
  946. /* if the decoder init function was already called previously,
  947. * free the already allocated subtitle_header before overwriting it */
  948. if (av_codec_is_decoder(codec))
  949. av_freep(&avctx->subtitle_header);
  950. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  951. ret = AVERROR(EINVAL);
  952. goto free_and_end;
  953. }
  954. avctx->codec = codec;
  955. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  956. avctx->codec_id == AV_CODEC_ID_NONE) {
  957. avctx->codec_type = codec->type;
  958. avctx->codec_id = codec->id;
  959. }
  960. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  961. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  962. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  963. ret = AVERROR(EINVAL);
  964. goto free_and_end;
  965. }
  966. avctx->frame_number = 0;
  967. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  968. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  969. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  970. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  971. AVCodec *codec2;
  972. av_log(NULL, AV_LOG_ERROR,
  973. "The %s '%s' is experimental but experimental codecs are not enabled, "
  974. "add '-strict %d' if you want to use it.\n",
  975. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  976. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  977. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  978. av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  979. codec_string, codec2->name);
  980. ret = AVERROR_EXPERIMENTAL;
  981. goto free_and_end;
  982. }
  983. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  984. (!avctx->time_base.num || !avctx->time_base.den)) {
  985. avctx->time_base.num = 1;
  986. avctx->time_base.den = avctx->sample_rate;
  987. }
  988. if (!HAVE_THREADS)
  989. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  990. if (CONFIG_FRAME_THREAD_ENCODER) {
  991. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  992. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  993. ff_lock_avcodec(avctx);
  994. if (ret < 0)
  995. goto free_and_end;
  996. }
  997. if (HAVE_THREADS && !avctx->thread_opaque
  998. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  999. ret = ff_thread_init(avctx);
  1000. if (ret < 0) {
  1001. goto free_and_end;
  1002. }
  1003. }
  1004. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  1005. avctx->thread_count = 1;
  1006. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1007. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1008. avctx->codec->max_lowres);
  1009. ret = AVERROR(EINVAL);
  1010. goto free_and_end;
  1011. }
  1012. if (av_codec_is_encoder(avctx->codec)) {
  1013. int i;
  1014. if (avctx->codec->sample_fmts) {
  1015. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1016. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1017. break;
  1018. if (avctx->channels == 1 &&
  1019. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1020. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1021. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1022. break;
  1023. }
  1024. }
  1025. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1026. char buf[128];
  1027. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1028. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1029. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1030. ret = AVERROR(EINVAL);
  1031. goto free_and_end;
  1032. }
  1033. }
  1034. if (avctx->codec->pix_fmts) {
  1035. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1036. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1037. break;
  1038. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1039. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1040. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1041. char buf[128];
  1042. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1043. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1044. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1045. ret = AVERROR(EINVAL);
  1046. goto free_and_end;
  1047. }
  1048. }
  1049. if (avctx->codec->supported_samplerates) {
  1050. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1051. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1052. break;
  1053. if (avctx->codec->supported_samplerates[i] == 0) {
  1054. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1055. avctx->sample_rate);
  1056. ret = AVERROR(EINVAL);
  1057. goto free_and_end;
  1058. }
  1059. }
  1060. if (avctx->codec->channel_layouts) {
  1061. if (!avctx->channel_layout) {
  1062. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1063. } else {
  1064. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1065. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1066. break;
  1067. if (avctx->codec->channel_layouts[i] == 0) {
  1068. char buf[512];
  1069. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1070. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1071. ret = AVERROR(EINVAL);
  1072. goto free_and_end;
  1073. }
  1074. }
  1075. }
  1076. if (avctx->channel_layout && avctx->channels) {
  1077. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1078. if (channels != avctx->channels) {
  1079. char buf[512];
  1080. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1081. av_log(avctx, AV_LOG_ERROR,
  1082. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1083. buf, channels, avctx->channels);
  1084. ret = AVERROR(EINVAL);
  1085. goto free_and_end;
  1086. }
  1087. } else if (avctx->channel_layout) {
  1088. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1089. }
  1090. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
  1091. avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
  1092. ) {
  1093. if (avctx->width <= 0 || avctx->height <= 0) {
  1094. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1095. ret = AVERROR(EINVAL);
  1096. goto free_and_end;
  1097. }
  1098. }
  1099. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1100. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1101. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1102. }
  1103. if (!avctx->rc_initial_buffer_occupancy)
  1104. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1105. }
  1106. avctx->pts_correction_num_faulty_pts =
  1107. avctx->pts_correction_num_faulty_dts = 0;
  1108. avctx->pts_correction_last_pts =
  1109. avctx->pts_correction_last_dts = INT64_MIN;
  1110. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1111. || avctx->internal->frame_thread_encoder)) {
  1112. ret = avctx->codec->init(avctx);
  1113. if (ret < 0) {
  1114. goto free_and_end;
  1115. }
  1116. }
  1117. ret=0;
  1118. if (av_codec_is_decoder(avctx->codec)) {
  1119. if (!avctx->bit_rate)
  1120. avctx->bit_rate = get_bit_rate(avctx);
  1121. /* validate channel layout from the decoder */
  1122. if (avctx->channel_layout) {
  1123. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1124. if (!avctx->channels)
  1125. avctx->channels = channels;
  1126. else if (channels != avctx->channels) {
  1127. char buf[512];
  1128. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1129. av_log(avctx, AV_LOG_WARNING,
  1130. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1131. "ignoring specified channel layout\n",
  1132. buf, channels, avctx->channels);
  1133. avctx->channel_layout = 0;
  1134. }
  1135. }
  1136. if (avctx->channels && avctx->channels < 0 ||
  1137. avctx->channels > FF_SANE_NB_CHANNELS) {
  1138. ret = AVERROR(EINVAL);
  1139. goto free_and_end;
  1140. }
  1141. if (avctx->sub_charenc) {
  1142. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1143. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1144. "supported with subtitles codecs\n");
  1145. ret = AVERROR(EINVAL);
  1146. goto free_and_end;
  1147. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1148. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1149. "subtitles character encoding will be ignored\n",
  1150. avctx->codec_descriptor->name);
  1151. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1152. } else {
  1153. /* input character encoding is set for a text based subtitle
  1154. * codec at this point */
  1155. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1156. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1157. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1158. #if CONFIG_ICONV
  1159. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1160. if (cd == (iconv_t)-1) {
  1161. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1162. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1163. ret = AVERROR(errno);
  1164. goto free_and_end;
  1165. }
  1166. iconv_close(cd);
  1167. #else
  1168. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1169. "conversion needs a libavcodec built with iconv support "
  1170. "for this codec\n");
  1171. ret = AVERROR(ENOSYS);
  1172. goto free_and_end;
  1173. #endif
  1174. }
  1175. }
  1176. }
  1177. }
  1178. end:
  1179. ff_unlock_avcodec();
  1180. if (options) {
  1181. av_dict_free(options);
  1182. *options = tmp;
  1183. }
  1184. return ret;
  1185. free_and_end:
  1186. av_dict_free(&tmp);
  1187. av_freep(&avctx->priv_data);
  1188. if (avctx->internal)
  1189. av_freep(&avctx->internal->pool);
  1190. av_freep(&avctx->internal);
  1191. avctx->codec = NULL;
  1192. goto end;
  1193. }
  1194. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  1195. {
  1196. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1197. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  1198. return AVERROR(EINVAL);
  1199. }
  1200. if (avctx) {
  1201. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1202. if (!avpkt->data || avpkt->size < size) {
  1203. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1204. avpkt->data = avctx->internal->byte_buffer;
  1205. avpkt->size = avctx->internal->byte_buffer_size;
  1206. avpkt->destruct = NULL;
  1207. }
  1208. }
  1209. if (avpkt->data) {
  1210. AVBufferRef *buf = avpkt->buf;
  1211. #if FF_API_DESTRUCT_PACKET
  1212. void *destruct = avpkt->destruct;
  1213. #endif
  1214. if (avpkt->size < size) {
  1215. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  1216. return AVERROR(EINVAL);
  1217. }
  1218. av_init_packet(avpkt);
  1219. #if FF_API_DESTRUCT_PACKET
  1220. avpkt->destruct = destruct;
  1221. #endif
  1222. avpkt->buf = buf;
  1223. avpkt->size = size;
  1224. return 0;
  1225. } else {
  1226. int ret = av_new_packet(avpkt, size);
  1227. if (ret < 0)
  1228. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  1229. return ret;
  1230. }
  1231. }
  1232. int ff_alloc_packet(AVPacket *avpkt, int size)
  1233. {
  1234. return ff_alloc_packet2(NULL, avpkt, size);
  1235. }
  1236. /**
  1237. * Pad last frame with silence.
  1238. */
  1239. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1240. {
  1241. AVFrame *frame = NULL;
  1242. uint8_t *buf = NULL;
  1243. int ret;
  1244. if (!(frame = avcodec_alloc_frame()))
  1245. return AVERROR(ENOMEM);
  1246. *frame = *src;
  1247. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  1248. s->frame_size, s->sample_fmt, 0)) < 0)
  1249. goto fail;
  1250. if (!(buf = av_malloc(ret))) {
  1251. ret = AVERROR(ENOMEM);
  1252. goto fail;
  1253. }
  1254. frame->nb_samples = s->frame_size;
  1255. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  1256. buf, ret, 0)) < 0)
  1257. goto fail;
  1258. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1259. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1260. goto fail;
  1261. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1262. frame->nb_samples - src->nb_samples,
  1263. s->channels, s->sample_fmt)) < 0)
  1264. goto fail;
  1265. *dst = frame;
  1266. return 0;
  1267. fail:
  1268. if (frame->extended_data != frame->data)
  1269. av_freep(&frame->extended_data);
  1270. av_freep(&buf);
  1271. av_freep(&frame);
  1272. return ret;
  1273. }
  1274. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1275. AVPacket *avpkt,
  1276. const AVFrame *frame,
  1277. int *got_packet_ptr)
  1278. {
  1279. AVFrame tmp;
  1280. AVFrame *padded_frame = NULL;
  1281. int ret;
  1282. AVPacket user_pkt = *avpkt;
  1283. int needs_realloc = !user_pkt.data;
  1284. *got_packet_ptr = 0;
  1285. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1286. av_free_packet(avpkt);
  1287. av_init_packet(avpkt);
  1288. return 0;
  1289. }
  1290. /* ensure that extended_data is properly set */
  1291. if (frame && !frame->extended_data) {
  1292. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1293. avctx->channels > AV_NUM_DATA_POINTERS) {
  1294. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1295. "with more than %d channels, but extended_data is not set.\n",
  1296. AV_NUM_DATA_POINTERS);
  1297. return AVERROR(EINVAL);
  1298. }
  1299. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1300. tmp = *frame;
  1301. tmp.extended_data = tmp.data;
  1302. frame = &tmp;
  1303. }
  1304. /* check for valid frame size */
  1305. if (frame) {
  1306. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1307. if (frame->nb_samples > avctx->frame_size) {
  1308. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1309. return AVERROR(EINVAL);
  1310. }
  1311. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1312. if (frame->nb_samples < avctx->frame_size &&
  1313. !avctx->internal->last_audio_frame) {
  1314. ret = pad_last_frame(avctx, &padded_frame, frame);
  1315. if (ret < 0)
  1316. return ret;
  1317. frame = padded_frame;
  1318. avctx->internal->last_audio_frame = 1;
  1319. }
  1320. if (frame->nb_samples != avctx->frame_size) {
  1321. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1322. ret = AVERROR(EINVAL);
  1323. goto end;
  1324. }
  1325. }
  1326. }
  1327. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1328. if (!ret) {
  1329. if (*got_packet_ptr) {
  1330. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1331. if (avpkt->pts == AV_NOPTS_VALUE)
  1332. avpkt->pts = frame->pts;
  1333. if (!avpkt->duration)
  1334. avpkt->duration = ff_samples_to_time_base(avctx,
  1335. frame->nb_samples);
  1336. }
  1337. avpkt->dts = avpkt->pts;
  1338. } else {
  1339. avpkt->size = 0;
  1340. }
  1341. }
  1342. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1343. needs_realloc = 0;
  1344. if (user_pkt.data) {
  1345. if (user_pkt.size >= avpkt->size) {
  1346. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1347. } else {
  1348. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1349. avpkt->size = user_pkt.size;
  1350. ret = -1;
  1351. }
  1352. avpkt->buf = user_pkt.buf;
  1353. avpkt->data = user_pkt.data;
  1354. avpkt->destruct = user_pkt.destruct;
  1355. } else {
  1356. if (av_dup_packet(avpkt) < 0) {
  1357. ret = AVERROR(ENOMEM);
  1358. }
  1359. }
  1360. }
  1361. if (!ret) {
  1362. if (needs_realloc && avpkt->data) {
  1363. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1364. if (ret >= 0)
  1365. avpkt->data = avpkt->buf->data;
  1366. }
  1367. avctx->frame_number++;
  1368. }
  1369. if (ret < 0 || !*got_packet_ptr) {
  1370. av_free_packet(avpkt);
  1371. av_init_packet(avpkt);
  1372. goto end;
  1373. }
  1374. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1375. * this needs to be moved to the encoders, but for now we can do it
  1376. * here to simplify things */
  1377. avpkt->flags |= AV_PKT_FLAG_KEY;
  1378. end:
  1379. if (padded_frame) {
  1380. av_freep(&padded_frame->data[0]);
  1381. if (padded_frame->extended_data != padded_frame->data)
  1382. av_freep(&padded_frame->extended_data);
  1383. av_freep(&padded_frame);
  1384. }
  1385. return ret;
  1386. }
  1387. #if FF_API_OLD_ENCODE_AUDIO
  1388. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1389. uint8_t *buf, int buf_size,
  1390. const short *samples)
  1391. {
  1392. AVPacket pkt;
  1393. AVFrame frame0 = { { 0 } };
  1394. AVFrame *frame;
  1395. int ret, samples_size, got_packet;
  1396. av_init_packet(&pkt);
  1397. pkt.data = buf;
  1398. pkt.size = buf_size;
  1399. if (samples) {
  1400. frame = &frame0;
  1401. avcodec_get_frame_defaults(frame);
  1402. if (avctx->frame_size) {
  1403. frame->nb_samples = avctx->frame_size;
  1404. } else {
  1405. /* if frame_size is not set, the number of samples must be
  1406. * calculated from the buffer size */
  1407. int64_t nb_samples;
  1408. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1409. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1410. "support this codec\n");
  1411. return AVERROR(EINVAL);
  1412. }
  1413. nb_samples = (int64_t)buf_size * 8 /
  1414. (av_get_bits_per_sample(avctx->codec_id) *
  1415. avctx->channels);
  1416. if (nb_samples >= INT_MAX)
  1417. return AVERROR(EINVAL);
  1418. frame->nb_samples = nb_samples;
  1419. }
  1420. /* it is assumed that the samples buffer is large enough based on the
  1421. * relevant parameters */
  1422. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1423. frame->nb_samples,
  1424. avctx->sample_fmt, 1);
  1425. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1426. avctx->sample_fmt,
  1427. (const uint8_t *)samples,
  1428. samples_size, 1)) < 0)
  1429. return ret;
  1430. /* fabricate frame pts from sample count.
  1431. * this is needed because the avcodec_encode_audio() API does not have
  1432. * a way for the user to provide pts */
  1433. if (avctx->sample_rate && avctx->time_base.num)
  1434. frame->pts = ff_samples_to_time_base(avctx,
  1435. avctx->internal->sample_count);
  1436. else
  1437. frame->pts = AV_NOPTS_VALUE;
  1438. avctx->internal->sample_count += frame->nb_samples;
  1439. } else {
  1440. frame = NULL;
  1441. }
  1442. got_packet = 0;
  1443. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1444. if (!ret && got_packet && avctx->coded_frame) {
  1445. avctx->coded_frame->pts = pkt.pts;
  1446. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1447. }
  1448. /* free any side data since we cannot return it */
  1449. ff_packet_free_side_data(&pkt);
  1450. if (frame && frame->extended_data != frame->data)
  1451. av_freep(&frame->extended_data);
  1452. return ret ? ret : pkt.size;
  1453. }
  1454. #endif
  1455. #if FF_API_OLD_ENCODE_VIDEO
  1456. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1457. const AVFrame *pict)
  1458. {
  1459. AVPacket pkt;
  1460. int ret, got_packet = 0;
  1461. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1462. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1463. return -1;
  1464. }
  1465. av_init_packet(&pkt);
  1466. pkt.data = buf;
  1467. pkt.size = buf_size;
  1468. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1469. if (!ret && got_packet && avctx->coded_frame) {
  1470. avctx->coded_frame->pts = pkt.pts;
  1471. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1472. }
  1473. /* free any side data since we cannot return it */
  1474. if (pkt.side_data_elems > 0) {
  1475. int i;
  1476. for (i = 0; i < pkt.side_data_elems; i++)
  1477. av_free(pkt.side_data[i].data);
  1478. av_freep(&pkt.side_data);
  1479. pkt.side_data_elems = 0;
  1480. }
  1481. return ret ? ret : pkt.size;
  1482. }
  1483. #endif
  1484. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1485. AVPacket *avpkt,
  1486. const AVFrame *frame,
  1487. int *got_packet_ptr)
  1488. {
  1489. int ret;
  1490. AVPacket user_pkt = *avpkt;
  1491. int needs_realloc = !user_pkt.data;
  1492. *got_packet_ptr = 0;
  1493. if(CONFIG_FRAME_THREAD_ENCODER &&
  1494. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1495. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1496. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1497. avctx->stats_out[0] = '\0';
  1498. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1499. av_free_packet(avpkt);
  1500. av_init_packet(avpkt);
  1501. avpkt->size = 0;
  1502. return 0;
  1503. }
  1504. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1505. return AVERROR(EINVAL);
  1506. av_assert0(avctx->codec->encode2);
  1507. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1508. av_assert0(ret <= 0);
  1509. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1510. needs_realloc = 0;
  1511. if (user_pkt.data) {
  1512. if (user_pkt.size >= avpkt->size) {
  1513. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1514. } else {
  1515. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1516. avpkt->size = user_pkt.size;
  1517. ret = -1;
  1518. }
  1519. avpkt->buf = user_pkt.buf;
  1520. avpkt->data = user_pkt.data;
  1521. avpkt->destruct = user_pkt.destruct;
  1522. } else {
  1523. if (av_dup_packet(avpkt) < 0) {
  1524. ret = AVERROR(ENOMEM);
  1525. }
  1526. }
  1527. }
  1528. if (!ret) {
  1529. if (!*got_packet_ptr)
  1530. avpkt->size = 0;
  1531. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1532. avpkt->pts = avpkt->dts = frame->pts;
  1533. if (needs_realloc && avpkt->data) {
  1534. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1535. if (ret >= 0)
  1536. avpkt->data = avpkt->buf->data;
  1537. }
  1538. avctx->frame_number++;
  1539. }
  1540. if (ret < 0 || !*got_packet_ptr)
  1541. av_free_packet(avpkt);
  1542. emms_c();
  1543. return ret;
  1544. }
  1545. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1546. const AVSubtitle *sub)
  1547. {
  1548. int ret;
  1549. if (sub->start_display_time) {
  1550. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1551. return -1;
  1552. }
  1553. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1554. avctx->frame_number++;
  1555. return ret;
  1556. }
  1557. /**
  1558. * Attempt to guess proper monotonic timestamps for decoded video frames
  1559. * which might have incorrect times. Input timestamps may wrap around, in
  1560. * which case the output will as well.
  1561. *
  1562. * @param pts the pts field of the decoded AVPacket, as passed through
  1563. * AVFrame.pkt_pts
  1564. * @param dts the dts field of the decoded AVPacket
  1565. * @return one of the input values, may be AV_NOPTS_VALUE
  1566. */
  1567. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1568. int64_t reordered_pts, int64_t dts)
  1569. {
  1570. int64_t pts = AV_NOPTS_VALUE;
  1571. if (dts != AV_NOPTS_VALUE) {
  1572. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1573. ctx->pts_correction_last_dts = dts;
  1574. }
  1575. if (reordered_pts != AV_NOPTS_VALUE) {
  1576. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1577. ctx->pts_correction_last_pts = reordered_pts;
  1578. }
  1579. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1580. && reordered_pts != AV_NOPTS_VALUE)
  1581. pts = reordered_pts;
  1582. else
  1583. pts = dts;
  1584. return pts;
  1585. }
  1586. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1587. {
  1588. int size = 0;
  1589. const uint8_t *data;
  1590. uint32_t flags;
  1591. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1592. return;
  1593. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1594. if (!data || size < 4)
  1595. return;
  1596. flags = bytestream_get_le32(&data);
  1597. size -= 4;
  1598. if (size < 4) /* Required for any of the changes */
  1599. return;
  1600. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1601. avctx->channels = bytestream_get_le32(&data);
  1602. size -= 4;
  1603. }
  1604. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1605. if (size < 8)
  1606. return;
  1607. avctx->channel_layout = bytestream_get_le64(&data);
  1608. size -= 8;
  1609. }
  1610. if (size < 4)
  1611. return;
  1612. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1613. avctx->sample_rate = bytestream_get_le32(&data);
  1614. size -= 4;
  1615. }
  1616. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1617. if (size < 8)
  1618. return;
  1619. avctx->width = bytestream_get_le32(&data);
  1620. avctx->height = bytestream_get_le32(&data);
  1621. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1622. size -= 8;
  1623. }
  1624. }
  1625. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1626. {
  1627. int size, ret = 0;
  1628. const uint8_t *side_metadata;
  1629. const uint8_t *end;
  1630. side_metadata = av_packet_get_side_data(avctx->pkt,
  1631. AV_PKT_DATA_STRINGS_METADATA, &size);
  1632. if (!side_metadata)
  1633. goto end;
  1634. end = side_metadata + size;
  1635. while (side_metadata < end) {
  1636. const uint8_t *key = side_metadata;
  1637. const uint8_t *val = side_metadata + strlen(key) + 1;
  1638. int ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
  1639. if (ret < 0)
  1640. break;
  1641. side_metadata = val + strlen(val) + 1;
  1642. }
  1643. end:
  1644. return ret;
  1645. }
  1646. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1647. int *got_picture_ptr,
  1648. const AVPacket *avpkt)
  1649. {
  1650. AVCodecInternal *avci = avctx->internal;
  1651. int ret;
  1652. // copy to ensure we do not change avpkt
  1653. AVPacket tmp = *avpkt;
  1654. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1655. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1656. return AVERROR(EINVAL);
  1657. }
  1658. *got_picture_ptr = 0;
  1659. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1660. return AVERROR(EINVAL);
  1661. avcodec_get_frame_defaults(picture);
  1662. if (!avctx->refcounted_frames)
  1663. av_frame_unref(&avci->to_free);
  1664. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1665. int did_split = av_packet_split_side_data(&tmp);
  1666. apply_param_change(avctx, &tmp);
  1667. avctx->pkt = &tmp;
  1668. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1669. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1670. &tmp);
  1671. else {
  1672. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1673. &tmp);
  1674. picture->pkt_dts = avpkt->dts;
  1675. if(!avctx->has_b_frames){
  1676. av_frame_set_pkt_pos(picture, avpkt->pos);
  1677. }
  1678. //FIXME these should be under if(!avctx->has_b_frames)
  1679. /* get_buffer is supposed to set frame parameters */
  1680. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1681. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1682. if (!picture->width) picture->width = avctx->width;
  1683. if (!picture->height) picture->height = avctx->height;
  1684. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1685. }
  1686. }
  1687. add_metadata_from_side_data(avctx, picture);
  1688. emms_c(); //needed to avoid an emms_c() call before every return;
  1689. avctx->pkt = NULL;
  1690. if (did_split) {
  1691. ff_packet_free_side_data(&tmp);
  1692. if(ret == tmp.size)
  1693. ret = avpkt->size;
  1694. }
  1695. if (ret < 0 && picture->data[0])
  1696. av_frame_unref(picture);
  1697. if (*got_picture_ptr) {
  1698. if (!avctx->refcounted_frames) {
  1699. avci->to_free = *picture;
  1700. avci->to_free.extended_data = avci->to_free.data;
  1701. }
  1702. avctx->frame_number++;
  1703. av_frame_set_best_effort_timestamp(picture,
  1704. guess_correct_pts(avctx,
  1705. picture->pkt_pts,
  1706. picture->pkt_dts));
  1707. }
  1708. } else
  1709. ret = 0;
  1710. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1711. * make sure it's set correctly */
  1712. picture->extended_data = picture->data;
  1713. return ret;
  1714. }
  1715. #if FF_API_OLD_DECODE_AUDIO
  1716. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1717. int *frame_size_ptr,
  1718. AVPacket *avpkt)
  1719. {
  1720. AVFrame frame = { { 0 } };
  1721. int ret, got_frame = 0;
  1722. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1723. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1724. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1725. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1726. "avcodec_decode_audio4()\n");
  1727. avctx->get_buffer = avcodec_default_get_buffer;
  1728. avctx->release_buffer = avcodec_default_release_buffer;
  1729. }
  1730. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1731. if (ret >= 0 && got_frame) {
  1732. int ch, plane_size;
  1733. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1734. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1735. frame.nb_samples,
  1736. avctx->sample_fmt, 1);
  1737. if (*frame_size_ptr < data_size) {
  1738. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1739. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1740. return AVERROR(EINVAL);
  1741. }
  1742. memcpy(samples, frame.extended_data[0], plane_size);
  1743. if (planar && avctx->channels > 1) {
  1744. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1745. for (ch = 1; ch < avctx->channels; ch++) {
  1746. memcpy(out, frame.extended_data[ch], plane_size);
  1747. out += plane_size;
  1748. }
  1749. }
  1750. *frame_size_ptr = data_size;
  1751. } else {
  1752. *frame_size_ptr = 0;
  1753. }
  1754. return ret;
  1755. }
  1756. #endif
  1757. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1758. AVFrame *frame,
  1759. int *got_frame_ptr,
  1760. const AVPacket *avpkt)
  1761. {
  1762. AVCodecInternal *avci = avctx->internal;
  1763. int planar, channels;
  1764. int ret = 0;
  1765. *got_frame_ptr = 0;
  1766. if (!avpkt->data && avpkt->size) {
  1767. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1768. return AVERROR(EINVAL);
  1769. }
  1770. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1771. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1772. return AVERROR(EINVAL);
  1773. }
  1774. avcodec_get_frame_defaults(frame);
  1775. if (!avctx->refcounted_frames)
  1776. av_frame_unref(&avci->to_free);
  1777. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1778. uint8_t *side;
  1779. int side_size;
  1780. // copy to ensure we do not change avpkt
  1781. AVPacket tmp = *avpkt;
  1782. int did_split = av_packet_split_side_data(&tmp);
  1783. apply_param_change(avctx, &tmp);
  1784. avctx->pkt = &tmp;
  1785. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1786. if (ret >= 0 && *got_frame_ptr) {
  1787. add_metadata_from_side_data(avctx, frame);
  1788. avctx->frame_number++;
  1789. frame->pkt_dts = avpkt->dts;
  1790. av_frame_set_best_effort_timestamp(frame,
  1791. guess_correct_pts(avctx,
  1792. frame->pkt_pts,
  1793. frame->pkt_dts));
  1794. if (frame->format == AV_SAMPLE_FMT_NONE)
  1795. frame->format = avctx->sample_fmt;
  1796. if (!frame->channel_layout)
  1797. frame->channel_layout = avctx->channel_layout;
  1798. if (!av_frame_get_channels(frame))
  1799. av_frame_set_channels(frame, avctx->channels);
  1800. if (!frame->sample_rate)
  1801. frame->sample_rate = avctx->sample_rate;
  1802. if (!avctx->refcounted_frames) {
  1803. avci->to_free = *frame;
  1804. avci->to_free.extended_data = avci->to_free.data;
  1805. }
  1806. }
  1807. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1808. if(side && side_size>=10) {
  1809. avctx->internal->skip_samples = AV_RL32(side);
  1810. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1811. avctx->internal->skip_samples);
  1812. }
  1813. if (avctx->internal->skip_samples && *got_frame_ptr) {
  1814. if(frame->nb_samples <= avctx->internal->skip_samples){
  1815. *got_frame_ptr = 0;
  1816. avctx->internal->skip_samples -= frame->nb_samples;
  1817. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1818. avctx->internal->skip_samples);
  1819. } else {
  1820. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1821. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1822. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1823. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1824. (AVRational){1, avctx->sample_rate},
  1825. avctx->pkt_timebase);
  1826. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1827. frame->pkt_pts += diff_ts;
  1828. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1829. frame->pkt_dts += diff_ts;
  1830. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1831. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1832. } else {
  1833. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1834. }
  1835. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1836. avctx->internal->skip_samples, frame->nb_samples);
  1837. frame->nb_samples -= avctx->internal->skip_samples;
  1838. avctx->internal->skip_samples = 0;
  1839. }
  1840. }
  1841. avctx->pkt = NULL;
  1842. if (did_split) {
  1843. ff_packet_free_side_data(&tmp);
  1844. if(ret == tmp.size)
  1845. ret = avpkt->size;
  1846. }
  1847. if (ret < 0 && frame->data[0])
  1848. av_frame_unref(frame);
  1849. }
  1850. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1851. * make sure it's set correctly; assume decoders that actually use
  1852. * extended_data are doing it correctly */
  1853. if (*got_frame_ptr) {
  1854. planar = av_sample_fmt_is_planar(frame->format);
  1855. channels = av_frame_get_channels(frame);
  1856. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1857. frame->extended_data = frame->data;
  1858. } else {
  1859. frame->extended_data = NULL;
  1860. }
  1861. return ret;
  1862. }
  1863. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  1864. static int recode_subtitle(AVCodecContext *avctx,
  1865. AVPacket *outpkt, const AVPacket *inpkt)
  1866. {
  1867. #if CONFIG_ICONV
  1868. iconv_t cd = (iconv_t)-1;
  1869. int ret = 0;
  1870. char *inb, *outb;
  1871. size_t inl, outl;
  1872. AVPacket tmp;
  1873. #endif
  1874. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER)
  1875. return 0;
  1876. #if CONFIG_ICONV
  1877. cd = iconv_open("UTF-8", avctx->sub_charenc);
  1878. av_assert0(cd != (iconv_t)-1);
  1879. inb = inpkt->data;
  1880. inl = inpkt->size;
  1881. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  1882. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  1883. ret = AVERROR(ENOMEM);
  1884. goto end;
  1885. }
  1886. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  1887. if (ret < 0)
  1888. goto end;
  1889. outpkt->buf = tmp.buf;
  1890. outpkt->data = tmp.data;
  1891. outpkt->size = tmp.size;
  1892. outb = outpkt->data;
  1893. outl = outpkt->size;
  1894. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  1895. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  1896. outl >= outpkt->size || inl != 0) {
  1897. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  1898. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  1899. av_free_packet(&tmp);
  1900. ret = AVERROR(errno);
  1901. goto end;
  1902. }
  1903. outpkt->size -= outl;
  1904. outpkt->data[outpkt->size - 1] = '\0';
  1905. end:
  1906. if (cd != (iconv_t)-1)
  1907. iconv_close(cd);
  1908. return ret;
  1909. #else
  1910. av_assert0(!"requesting subtitles recoding without iconv");
  1911. #endif
  1912. }
  1913. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1914. int *got_sub_ptr,
  1915. AVPacket *avpkt)
  1916. {
  1917. int ret = 0;
  1918. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1919. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1920. return AVERROR(EINVAL);
  1921. }
  1922. *got_sub_ptr = 0;
  1923. avcodec_get_subtitle_defaults(sub);
  1924. if (avpkt->size) {
  1925. AVPacket pkt_recoded;
  1926. AVPacket tmp = *avpkt;
  1927. int did_split = av_packet_split_side_data(&tmp);
  1928. //apply_param_change(avctx, &tmp);
  1929. pkt_recoded = tmp;
  1930. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  1931. if (ret < 0) {
  1932. *got_sub_ptr = 0;
  1933. } else {
  1934. avctx->pkt = &pkt_recoded;
  1935. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1936. sub->pts = av_rescale_q(avpkt->pts,
  1937. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1938. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  1939. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  1940. !!*got_sub_ptr >= !!sub->num_rects);
  1941. if (tmp.data != pkt_recoded.data) { // did we recode?
  1942. /* prevent from destroying side data from original packet */
  1943. pkt_recoded.side_data = NULL;
  1944. pkt_recoded.side_data_elems = 0;
  1945. av_free_packet(&pkt_recoded);
  1946. }
  1947. sub->format = !(avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB);
  1948. avctx->pkt = NULL;
  1949. }
  1950. if (did_split) {
  1951. ff_packet_free_side_data(&tmp);
  1952. if(ret == tmp.size)
  1953. ret = avpkt->size;
  1954. }
  1955. if (*got_sub_ptr)
  1956. avctx->frame_number++;
  1957. }
  1958. return ret;
  1959. }
  1960. void avsubtitle_free(AVSubtitle *sub)
  1961. {
  1962. int i;
  1963. for (i = 0; i < sub->num_rects; i++) {
  1964. av_freep(&sub->rects[i]->pict.data[0]);
  1965. av_freep(&sub->rects[i]->pict.data[1]);
  1966. av_freep(&sub->rects[i]->pict.data[2]);
  1967. av_freep(&sub->rects[i]->pict.data[3]);
  1968. av_freep(&sub->rects[i]->text);
  1969. av_freep(&sub->rects[i]->ass);
  1970. av_freep(&sub->rects[i]);
  1971. }
  1972. av_freep(&sub->rects);
  1973. memset(sub, 0, sizeof(AVSubtitle));
  1974. }
  1975. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  1976. {
  1977. int ret = 0;
  1978. ff_unlock_avcodec();
  1979. ret = avcodec_close(avctx);
  1980. ff_lock_avcodec(NULL);
  1981. return ret;
  1982. }
  1983. av_cold int avcodec_close(AVCodecContext *avctx)
  1984. {
  1985. int ret = ff_lock_avcodec(avctx);
  1986. if (ret < 0)
  1987. return ret;
  1988. if (avcodec_is_open(avctx)) {
  1989. FramePool *pool = avctx->internal->pool;
  1990. int i;
  1991. if (CONFIG_FRAME_THREAD_ENCODER &&
  1992. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1993. ff_unlock_avcodec();
  1994. ff_frame_thread_encoder_free(avctx);
  1995. ff_lock_avcodec(avctx);
  1996. }
  1997. if (HAVE_THREADS && avctx->thread_opaque)
  1998. ff_thread_free(avctx);
  1999. if (avctx->codec && avctx->codec->close)
  2000. avctx->codec->close(avctx);
  2001. avctx->coded_frame = NULL;
  2002. avctx->internal->byte_buffer_size = 0;
  2003. av_freep(&avctx->internal->byte_buffer);
  2004. if (!avctx->refcounted_frames)
  2005. av_frame_unref(&avctx->internal->to_free);
  2006. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2007. av_buffer_pool_uninit(&pool->pools[i]);
  2008. av_freep(&avctx->internal->pool);
  2009. av_freep(&avctx->internal);
  2010. }
  2011. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2012. av_opt_free(avctx->priv_data);
  2013. av_opt_free(avctx);
  2014. av_freep(&avctx->priv_data);
  2015. if (av_codec_is_encoder(avctx->codec))
  2016. av_freep(&avctx->extradata);
  2017. avctx->codec = NULL;
  2018. avctx->active_thread_type = 0;
  2019. ff_unlock_avcodec();
  2020. return 0;
  2021. }
  2022. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2023. {
  2024. switch(id){
  2025. //This is for future deprecatec codec ids, its empty since
  2026. //last major bump but will fill up again over time, please don't remove it
  2027. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2028. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  2029. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2030. default : return id;
  2031. }
  2032. }
  2033. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2034. {
  2035. AVCodec *p, *experimental = NULL;
  2036. p = first_avcodec;
  2037. id= remap_deprecated_codec_id(id);
  2038. while (p) {
  2039. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2040. p->id == id) {
  2041. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  2042. experimental = p;
  2043. } else
  2044. return p;
  2045. }
  2046. p = p->next;
  2047. }
  2048. return experimental;
  2049. }
  2050. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2051. {
  2052. return find_encdec(id, 1);
  2053. }
  2054. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2055. {
  2056. AVCodec *p;
  2057. if (!name)
  2058. return NULL;
  2059. p = first_avcodec;
  2060. while (p) {
  2061. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2062. return p;
  2063. p = p->next;
  2064. }
  2065. return NULL;
  2066. }
  2067. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2068. {
  2069. return find_encdec(id, 0);
  2070. }
  2071. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2072. {
  2073. AVCodec *p;
  2074. if (!name)
  2075. return NULL;
  2076. p = first_avcodec;
  2077. while (p) {
  2078. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2079. return p;
  2080. p = p->next;
  2081. }
  2082. return NULL;
  2083. }
  2084. const char *avcodec_get_name(enum AVCodecID id)
  2085. {
  2086. const AVCodecDescriptor *cd;
  2087. AVCodec *codec;
  2088. if (id == AV_CODEC_ID_NONE)
  2089. return "none";
  2090. cd = avcodec_descriptor_get(id);
  2091. if (cd)
  2092. return cd->name;
  2093. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2094. codec = avcodec_find_decoder(id);
  2095. if (codec)
  2096. return codec->name;
  2097. codec = avcodec_find_encoder(id);
  2098. if (codec)
  2099. return codec->name;
  2100. return "unknown_codec";
  2101. }
  2102. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2103. {
  2104. int i, len, ret = 0;
  2105. #define TAG_PRINT(x) \
  2106. (((x) >= '0' && (x) <= '9') || \
  2107. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2108. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2109. for (i = 0; i < 4; i++) {
  2110. len = snprintf(buf, buf_size,
  2111. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2112. buf += len;
  2113. buf_size = buf_size > len ? buf_size - len : 0;
  2114. ret += len;
  2115. codec_tag >>= 8;
  2116. }
  2117. return ret;
  2118. }
  2119. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2120. {
  2121. const char *codec_type;
  2122. const char *codec_name;
  2123. const char *profile = NULL;
  2124. const AVCodec *p;
  2125. int bitrate;
  2126. AVRational display_aspect_ratio;
  2127. if (!buf || buf_size <= 0)
  2128. return;
  2129. codec_type = av_get_media_type_string(enc->codec_type);
  2130. codec_name = avcodec_get_name(enc->codec_id);
  2131. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2132. if (enc->codec)
  2133. p = enc->codec;
  2134. else
  2135. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2136. avcodec_find_decoder(enc->codec_id);
  2137. if (p)
  2138. profile = av_get_profile_name(p, enc->profile);
  2139. }
  2140. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  2141. codec_name, enc->mb_decision ? " (hq)" : "");
  2142. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2143. if (profile)
  2144. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2145. if (enc->codec_tag) {
  2146. char tag_buf[32];
  2147. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2148. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2149. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2150. }
  2151. switch (enc->codec_type) {
  2152. case AVMEDIA_TYPE_VIDEO:
  2153. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  2154. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2155. ", %s",
  2156. av_get_pix_fmt_name(enc->pix_fmt));
  2157. if (enc->bits_per_raw_sample &&
  2158. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2159. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2160. " (%d bpc)", enc->bits_per_raw_sample);
  2161. }
  2162. if (enc->width) {
  2163. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2164. ", %dx%d",
  2165. enc->width, enc->height);
  2166. if (enc->sample_aspect_ratio.num) {
  2167. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2168. enc->width * enc->sample_aspect_ratio.num,
  2169. enc->height * enc->sample_aspect_ratio.den,
  2170. 1024 * 1024);
  2171. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2172. " [SAR %d:%d DAR %d:%d]",
  2173. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2174. display_aspect_ratio.num, display_aspect_ratio.den);
  2175. }
  2176. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2177. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2178. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2179. ", %d/%d",
  2180. enc->time_base.num / g, enc->time_base.den / g);
  2181. }
  2182. }
  2183. if (encode) {
  2184. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2185. ", q=%d-%d", enc->qmin, enc->qmax);
  2186. }
  2187. break;
  2188. case AVMEDIA_TYPE_AUDIO:
  2189. if (enc->sample_rate) {
  2190. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2191. ", %d Hz", enc->sample_rate);
  2192. }
  2193. av_strlcat(buf, ", ", buf_size);
  2194. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2195. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2196. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2197. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2198. }
  2199. break;
  2200. case AVMEDIA_TYPE_DATA:
  2201. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2202. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2203. if (g)
  2204. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2205. ", %d/%d",
  2206. enc->time_base.num / g, enc->time_base.den / g);
  2207. }
  2208. break;
  2209. default:
  2210. return;
  2211. }
  2212. if (encode) {
  2213. if (enc->flags & CODEC_FLAG_PASS1)
  2214. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2215. ", pass 1");
  2216. if (enc->flags & CODEC_FLAG_PASS2)
  2217. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2218. ", pass 2");
  2219. }
  2220. bitrate = get_bit_rate(enc);
  2221. if (bitrate != 0) {
  2222. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2223. ", %d kb/s", bitrate / 1000);
  2224. }
  2225. }
  2226. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2227. {
  2228. const AVProfile *p;
  2229. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2230. return NULL;
  2231. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2232. if (p->profile == profile)
  2233. return p->name;
  2234. return NULL;
  2235. }
  2236. unsigned avcodec_version(void)
  2237. {
  2238. // av_assert0(AV_CODEC_ID_V410==164);
  2239. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2240. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2241. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2242. av_assert0(AV_CODEC_ID_SRT==94216);
  2243. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2244. av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2245. av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2246. av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2247. av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2248. av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2249. return LIBAVCODEC_VERSION_INT;
  2250. }
  2251. const char *avcodec_configuration(void)
  2252. {
  2253. return FFMPEG_CONFIGURATION;
  2254. }
  2255. const char *avcodec_license(void)
  2256. {
  2257. #define LICENSE_PREFIX "libavcodec license: "
  2258. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2259. }
  2260. void avcodec_flush_buffers(AVCodecContext *avctx)
  2261. {
  2262. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2263. ff_thread_flush(avctx);
  2264. else if (avctx->codec->flush)
  2265. avctx->codec->flush(avctx);
  2266. avctx->pts_correction_last_pts =
  2267. avctx->pts_correction_last_dts = INT64_MIN;
  2268. }
  2269. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2270. {
  2271. switch (codec_id) {
  2272. case AV_CODEC_ID_8SVX_EXP:
  2273. case AV_CODEC_ID_8SVX_FIB:
  2274. case AV_CODEC_ID_ADPCM_CT:
  2275. case AV_CODEC_ID_ADPCM_IMA_APC:
  2276. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2277. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2278. case AV_CODEC_ID_ADPCM_IMA_WS:
  2279. case AV_CODEC_ID_ADPCM_G722:
  2280. case AV_CODEC_ID_ADPCM_YAMAHA:
  2281. return 4;
  2282. case AV_CODEC_ID_PCM_ALAW:
  2283. case AV_CODEC_ID_PCM_MULAW:
  2284. case AV_CODEC_ID_PCM_S8:
  2285. case AV_CODEC_ID_PCM_S8_PLANAR:
  2286. case AV_CODEC_ID_PCM_U8:
  2287. case AV_CODEC_ID_PCM_ZORK:
  2288. return 8;
  2289. case AV_CODEC_ID_PCM_S16BE:
  2290. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2291. case AV_CODEC_ID_PCM_S16LE:
  2292. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2293. case AV_CODEC_ID_PCM_U16BE:
  2294. case AV_CODEC_ID_PCM_U16LE:
  2295. return 16;
  2296. case AV_CODEC_ID_PCM_S24DAUD:
  2297. case AV_CODEC_ID_PCM_S24BE:
  2298. case AV_CODEC_ID_PCM_S24LE:
  2299. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2300. case AV_CODEC_ID_PCM_U24BE:
  2301. case AV_CODEC_ID_PCM_U24LE:
  2302. return 24;
  2303. case AV_CODEC_ID_PCM_S32BE:
  2304. case AV_CODEC_ID_PCM_S32LE:
  2305. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2306. case AV_CODEC_ID_PCM_U32BE:
  2307. case AV_CODEC_ID_PCM_U32LE:
  2308. case AV_CODEC_ID_PCM_F32BE:
  2309. case AV_CODEC_ID_PCM_F32LE:
  2310. return 32;
  2311. case AV_CODEC_ID_PCM_F64BE:
  2312. case AV_CODEC_ID_PCM_F64LE:
  2313. return 64;
  2314. default:
  2315. return 0;
  2316. }
  2317. }
  2318. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2319. {
  2320. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2321. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2322. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2323. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2324. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2325. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2326. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2327. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2328. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2329. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2330. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2331. };
  2332. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2333. return AV_CODEC_ID_NONE;
  2334. if (be < 0 || be > 1)
  2335. be = AV_NE(1, 0);
  2336. return map[fmt][be];
  2337. }
  2338. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2339. {
  2340. switch (codec_id) {
  2341. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2342. return 2;
  2343. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2344. return 3;
  2345. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2346. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2347. case AV_CODEC_ID_ADPCM_IMA_QT:
  2348. case AV_CODEC_ID_ADPCM_SWF:
  2349. case AV_CODEC_ID_ADPCM_MS:
  2350. return 4;
  2351. default:
  2352. return av_get_exact_bits_per_sample(codec_id);
  2353. }
  2354. }
  2355. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2356. {
  2357. int id, sr, ch, ba, tag, bps;
  2358. id = avctx->codec_id;
  2359. sr = avctx->sample_rate;
  2360. ch = avctx->channels;
  2361. ba = avctx->block_align;
  2362. tag = avctx->codec_tag;
  2363. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2364. /* codecs with an exact constant bits per sample */
  2365. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2366. return (frame_bytes * 8LL) / (bps * ch);
  2367. bps = avctx->bits_per_coded_sample;
  2368. /* codecs with a fixed packet duration */
  2369. switch (id) {
  2370. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2371. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2372. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2373. case AV_CODEC_ID_AMR_NB:
  2374. case AV_CODEC_ID_EVRC:
  2375. case AV_CODEC_ID_GSM:
  2376. case AV_CODEC_ID_QCELP:
  2377. case AV_CODEC_ID_RA_288: return 160;
  2378. case AV_CODEC_ID_AMR_WB:
  2379. case AV_CODEC_ID_GSM_MS: return 320;
  2380. case AV_CODEC_ID_MP1: return 384;
  2381. case AV_CODEC_ID_ATRAC1: return 512;
  2382. case AV_CODEC_ID_ATRAC3: return 1024;
  2383. case AV_CODEC_ID_MP2:
  2384. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2385. case AV_CODEC_ID_AC3: return 1536;
  2386. }
  2387. if (sr > 0) {
  2388. /* calc from sample rate */
  2389. if (id == AV_CODEC_ID_TTA)
  2390. return 256 * sr / 245;
  2391. if (ch > 0) {
  2392. /* calc from sample rate and channels */
  2393. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2394. return (480 << (sr / 22050)) / ch;
  2395. }
  2396. }
  2397. if (ba > 0) {
  2398. /* calc from block_align */
  2399. if (id == AV_CODEC_ID_SIPR) {
  2400. switch (ba) {
  2401. case 20: return 160;
  2402. case 19: return 144;
  2403. case 29: return 288;
  2404. case 37: return 480;
  2405. }
  2406. } else if (id == AV_CODEC_ID_ILBC) {
  2407. switch (ba) {
  2408. case 38: return 160;
  2409. case 50: return 240;
  2410. }
  2411. }
  2412. }
  2413. if (frame_bytes > 0) {
  2414. /* calc from frame_bytes only */
  2415. if (id == AV_CODEC_ID_TRUESPEECH)
  2416. return 240 * (frame_bytes / 32);
  2417. if (id == AV_CODEC_ID_NELLYMOSER)
  2418. return 256 * (frame_bytes / 64);
  2419. if (id == AV_CODEC_ID_RA_144)
  2420. return 160 * (frame_bytes / 20);
  2421. if (id == AV_CODEC_ID_G723_1)
  2422. return 240 * (frame_bytes / 24);
  2423. if (bps > 0) {
  2424. /* calc from frame_bytes and bits_per_coded_sample */
  2425. if (id == AV_CODEC_ID_ADPCM_G726)
  2426. return frame_bytes * 8 / bps;
  2427. }
  2428. if (ch > 0) {
  2429. /* calc from frame_bytes and channels */
  2430. switch (id) {
  2431. case AV_CODEC_ID_ADPCM_AFC:
  2432. return frame_bytes / (9 * ch) * 16;
  2433. case AV_CODEC_ID_ADPCM_4XM:
  2434. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2435. return (frame_bytes - 4 * ch) * 2 / ch;
  2436. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2437. return (frame_bytes - 4) * 2 / ch;
  2438. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2439. return (frame_bytes - 8) * 2 / ch;
  2440. case AV_CODEC_ID_ADPCM_XA:
  2441. return (frame_bytes / 128) * 224 / ch;
  2442. case AV_CODEC_ID_INTERPLAY_DPCM:
  2443. return (frame_bytes - 6 - ch) / ch;
  2444. case AV_CODEC_ID_ROQ_DPCM:
  2445. return (frame_bytes - 8) / ch;
  2446. case AV_CODEC_ID_XAN_DPCM:
  2447. return (frame_bytes - 2 * ch) / ch;
  2448. case AV_CODEC_ID_MACE3:
  2449. return 3 * frame_bytes / ch;
  2450. case AV_CODEC_ID_MACE6:
  2451. return 6 * frame_bytes / ch;
  2452. case AV_CODEC_ID_PCM_LXF:
  2453. return 2 * (frame_bytes / (5 * ch));
  2454. case AV_CODEC_ID_IAC:
  2455. case AV_CODEC_ID_IMC:
  2456. return 4 * frame_bytes / ch;
  2457. }
  2458. if (tag) {
  2459. /* calc from frame_bytes, channels, and codec_tag */
  2460. if (id == AV_CODEC_ID_SOL_DPCM) {
  2461. if (tag == 3)
  2462. return frame_bytes / ch;
  2463. else
  2464. return frame_bytes * 2 / ch;
  2465. }
  2466. }
  2467. if (ba > 0) {
  2468. /* calc from frame_bytes, channels, and block_align */
  2469. int blocks = frame_bytes / ba;
  2470. switch (avctx->codec_id) {
  2471. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2472. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2473. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2474. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2475. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2476. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2477. case AV_CODEC_ID_ADPCM_MS:
  2478. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2479. }
  2480. }
  2481. if (bps > 0) {
  2482. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2483. switch (avctx->codec_id) {
  2484. case AV_CODEC_ID_PCM_DVD:
  2485. if(bps<4)
  2486. return 0;
  2487. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2488. case AV_CODEC_ID_PCM_BLURAY:
  2489. if(bps<4)
  2490. return 0;
  2491. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2492. case AV_CODEC_ID_S302M:
  2493. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2494. }
  2495. }
  2496. }
  2497. }
  2498. return 0;
  2499. }
  2500. #if !HAVE_THREADS
  2501. int ff_thread_init(AVCodecContext *s)
  2502. {
  2503. return -1;
  2504. }
  2505. #endif
  2506. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2507. {
  2508. unsigned int n = 0;
  2509. while (v >= 0xff) {
  2510. *s++ = 0xff;
  2511. v -= 0xff;
  2512. n++;
  2513. }
  2514. *s = v;
  2515. n++;
  2516. return n;
  2517. }
  2518. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2519. {
  2520. int i;
  2521. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2522. return i;
  2523. }
  2524. #if FF_API_MISSING_SAMPLE
  2525. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2526. {
  2527. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2528. "version to the newest one from Git. If the problem still "
  2529. "occurs, it means that your file has a feature which has not "
  2530. "been implemented.\n", feature);
  2531. if(want_sample)
  2532. av_log_ask_for_sample(avc, NULL);
  2533. }
  2534. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2535. {
  2536. va_list argument_list;
  2537. va_start(argument_list, msg);
  2538. if (msg)
  2539. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2540. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2541. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2542. "and contact the ffmpeg-devel mailing list.\n");
  2543. va_end(argument_list);
  2544. }
  2545. #endif /* FF_API_MISSING_SAMPLE */
  2546. static AVHWAccel *first_hwaccel = NULL;
  2547. void av_register_hwaccel(AVHWAccel *hwaccel)
  2548. {
  2549. AVHWAccel **p = &first_hwaccel;
  2550. while (*p)
  2551. p = &(*p)->next;
  2552. *p = hwaccel;
  2553. hwaccel->next = NULL;
  2554. }
  2555. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2556. {
  2557. return hwaccel ? hwaccel->next : first_hwaccel;
  2558. }
  2559. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2560. {
  2561. AVHWAccel *hwaccel = NULL;
  2562. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2563. if (hwaccel->id == codec_id
  2564. && hwaccel->pix_fmt == pix_fmt)
  2565. return hwaccel;
  2566. return NULL;
  2567. }
  2568. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2569. {
  2570. if (ff_lockmgr_cb) {
  2571. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2572. return -1;
  2573. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2574. return -1;
  2575. }
  2576. ff_lockmgr_cb = cb;
  2577. if (ff_lockmgr_cb) {
  2578. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2579. return -1;
  2580. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2581. return -1;
  2582. }
  2583. return 0;
  2584. }
  2585. int ff_lock_avcodec(AVCodecContext *log_ctx)
  2586. {
  2587. if (ff_lockmgr_cb) {
  2588. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2589. return -1;
  2590. }
  2591. entangled_thread_counter++;
  2592. if (entangled_thread_counter != 1) {
  2593. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  2594. ff_avcodec_locked = 1;
  2595. ff_unlock_avcodec();
  2596. return AVERROR(EINVAL);
  2597. }
  2598. av_assert0(!ff_avcodec_locked);
  2599. ff_avcodec_locked = 1;
  2600. return 0;
  2601. }
  2602. int ff_unlock_avcodec(void)
  2603. {
  2604. av_assert0(ff_avcodec_locked);
  2605. ff_avcodec_locked = 0;
  2606. entangled_thread_counter--;
  2607. if (ff_lockmgr_cb) {
  2608. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2609. return -1;
  2610. }
  2611. return 0;
  2612. }
  2613. int avpriv_lock_avformat(void)
  2614. {
  2615. if (ff_lockmgr_cb) {
  2616. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2617. return -1;
  2618. }
  2619. return 0;
  2620. }
  2621. int avpriv_unlock_avformat(void)
  2622. {
  2623. if (ff_lockmgr_cb) {
  2624. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2625. return -1;
  2626. }
  2627. return 0;
  2628. }
  2629. unsigned int avpriv_toupper4(unsigned int x)
  2630. {
  2631. return av_toupper(x & 0xFF) +
  2632. (av_toupper((x >> 8) & 0xFF) << 8) +
  2633. (av_toupper((x >> 16) & 0xFF) << 16) +
  2634. (av_toupper((x >> 24) & 0xFF) << 24);
  2635. }
  2636. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2637. {
  2638. int ret;
  2639. dst->owner = src->owner;
  2640. ret = av_frame_ref(dst->f, src->f);
  2641. if (ret < 0)
  2642. return ret;
  2643. if (src->progress &&
  2644. !(dst->progress = av_buffer_ref(src->progress))) {
  2645. ff_thread_release_buffer(dst->owner, dst);
  2646. return AVERROR(ENOMEM);
  2647. }
  2648. return 0;
  2649. }
  2650. #if !HAVE_THREADS
  2651. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2652. {
  2653. f->owner = avctx;
  2654. return ff_get_buffer(avctx, f->f, flags);
  2655. }
  2656. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2657. {
  2658. av_frame_unref(f->f);
  2659. }
  2660. void ff_thread_finish_setup(AVCodecContext *avctx)
  2661. {
  2662. }
  2663. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2664. {
  2665. }
  2666. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2667. {
  2668. }
  2669. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2670. {
  2671. return 1;
  2672. }
  2673. #endif
  2674. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2675. {
  2676. AVCodec *c= avcodec_find_decoder(codec_id);
  2677. if(!c)
  2678. c= avcodec_find_encoder(codec_id);
  2679. if(c)
  2680. return c->type;
  2681. if (codec_id <= AV_CODEC_ID_NONE)
  2682. return AVMEDIA_TYPE_UNKNOWN;
  2683. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2684. return AVMEDIA_TYPE_VIDEO;
  2685. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2686. return AVMEDIA_TYPE_AUDIO;
  2687. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2688. return AVMEDIA_TYPE_SUBTITLE;
  2689. return AVMEDIA_TYPE_UNKNOWN;
  2690. }
  2691. int avcodec_is_open(AVCodecContext *s)
  2692. {
  2693. return !!s->internal;
  2694. }
  2695. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2696. {
  2697. int ret;
  2698. char *str;
  2699. ret = av_bprint_finalize(buf, &str);
  2700. if (ret < 0)
  2701. return ret;
  2702. avctx->extradata = str;
  2703. /* Note: the string is NUL terminated (so extradata can be read as a
  2704. * string), but the ending character is not accounted in the size (in
  2705. * binary formats you are likely not supposed to mux that character). When
  2706. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  2707. * zeros. */
  2708. avctx->extradata_size = buf->len;
  2709. return 0;
  2710. }
  2711. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  2712. const uint8_t *end,
  2713. uint32_t *av_restrict state)
  2714. {
  2715. int i;
  2716. assert(p <= end);
  2717. if (p >= end)
  2718. return end;
  2719. for (i = 0; i < 3; i++) {
  2720. uint32_t tmp = *state << 8;
  2721. *state = tmp + *(p++);
  2722. if (tmp == 0x100 || p == end)
  2723. return p;
  2724. }
  2725. while (p < end) {
  2726. if (p[-1] > 1 ) p += 3;
  2727. else if (p[-2] ) p += 2;
  2728. else if (p[-3]|(p[-1]-1)) p++;
  2729. else {
  2730. p++;
  2731. break;
  2732. }
  2733. }
  2734. p = FFMIN(p, end) - 4;
  2735. *state = AV_RB32(p);
  2736. return p + 4;
  2737. }