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.

3195 lines
103KB

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