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