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.

4289 lines
141KB

  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/atomic.h"
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/bprint.h"
  32. #include "libavutil/channel_layout.h"
  33. #include "libavutil/crc.h"
  34. #include "libavutil/frame.h"
  35. #include "libavutil/hwcontext.h"
  36. #include "libavutil/internal.h"
  37. #include "libavutil/mathematics.h"
  38. #include "libavutil/mem_internal.h"
  39. #include "libavutil/pixdesc.h"
  40. #include "libavutil/imgutils.h"
  41. #include "libavutil/samplefmt.h"
  42. #include "libavutil/dict.h"
  43. #include "libavutil/thread.h"
  44. #include "avcodec.h"
  45. #include "libavutil/opt.h"
  46. #include "me_cmp.h"
  47. #include "mpegvideo.h"
  48. #include "thread.h"
  49. #include "frame_thread_encoder.h"
  50. #include "internal.h"
  51. #include "raw.h"
  52. #include "bytestream.h"
  53. #include "version.h"
  54. #include <stdlib.h>
  55. #include <stdarg.h>
  56. #include <limits.h>
  57. #include <float.h>
  58. #if CONFIG_ICONV
  59. # include <iconv.h>
  60. #endif
  61. #include "libavutil/ffversion.h"
  62. const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  63. #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
  64. static int default_lockmgr_cb(void **arg, enum AVLockOp op)
  65. {
  66. void * volatile * mutex = arg;
  67. int err;
  68. switch (op) {
  69. case AV_LOCK_CREATE:
  70. return 0;
  71. case AV_LOCK_OBTAIN:
  72. if (!*mutex) {
  73. pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
  74. if (!tmp)
  75. return AVERROR(ENOMEM);
  76. if ((err = pthread_mutex_init(tmp, NULL))) {
  77. av_free(tmp);
  78. return AVERROR(err);
  79. }
  80. if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
  81. pthread_mutex_destroy(tmp);
  82. av_free(tmp);
  83. }
  84. }
  85. if ((err = pthread_mutex_lock(*mutex)))
  86. return AVERROR(err);
  87. return 0;
  88. case AV_LOCK_RELEASE:
  89. if ((err = pthread_mutex_unlock(*mutex)))
  90. return AVERROR(err);
  91. return 0;
  92. case AV_LOCK_DESTROY:
  93. if (*mutex)
  94. pthread_mutex_destroy(*mutex);
  95. av_free(*mutex);
  96. avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
  97. return 0;
  98. }
  99. return 1;
  100. }
  101. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
  102. #else
  103. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
  104. #endif
  105. volatile int ff_avcodec_locked;
  106. static int volatile entangled_thread_counter = 0;
  107. static void *codec_mutex;
  108. static void *avformat_mutex;
  109. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  110. {
  111. uint8_t **p = ptr;
  112. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  113. av_freep(p);
  114. *size = 0;
  115. return;
  116. }
  117. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  118. memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  119. }
  120. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  121. {
  122. uint8_t **p = ptr;
  123. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  124. av_freep(p);
  125. *size = 0;
  126. return;
  127. }
  128. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  129. memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  130. }
  131. /* encoder management */
  132. static AVCodec *first_avcodec = NULL;
  133. static AVCodec **last_avcodec = &first_avcodec;
  134. AVCodec *av_codec_next(const AVCodec *c)
  135. {
  136. if (c)
  137. return c->next;
  138. else
  139. return first_avcodec;
  140. }
  141. static av_cold void avcodec_init(void)
  142. {
  143. static int initialized = 0;
  144. if (initialized != 0)
  145. return;
  146. initialized = 1;
  147. if (CONFIG_ME_CMP)
  148. ff_me_cmp_init_static();
  149. }
  150. int av_codec_is_encoder(const AVCodec *codec)
  151. {
  152. return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
  153. }
  154. int av_codec_is_decoder(const AVCodec *codec)
  155. {
  156. return codec && (codec->decode || codec->send_packet);
  157. }
  158. av_cold void avcodec_register(AVCodec *codec)
  159. {
  160. AVCodec **p;
  161. avcodec_init();
  162. p = last_avcodec;
  163. codec->next = NULL;
  164. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
  165. p = &(*p)->next;
  166. last_avcodec = &codec->next;
  167. if (codec->init_static_data)
  168. codec->init_static_data(codec);
  169. }
  170. #if FF_API_EMU_EDGE
  171. unsigned avcodec_get_edge_width(void)
  172. {
  173. return EDGE_WIDTH;
  174. }
  175. #endif
  176. #if FF_API_SET_DIMENSIONS
  177. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  178. {
  179. int ret = ff_set_dimensions(s, width, height);
  180. if (ret < 0) {
  181. av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
  182. }
  183. }
  184. #endif
  185. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  186. {
  187. int ret = av_image_check_size(width, height, 0, s);
  188. if (ret < 0)
  189. width = height = 0;
  190. s->coded_width = width;
  191. s->coded_height = height;
  192. s->width = AV_CEIL_RSHIFT(width, s->lowres);
  193. s->height = AV_CEIL_RSHIFT(height, s->lowres);
  194. return ret;
  195. }
  196. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  197. {
  198. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  199. if (ret < 0) {
  200. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  201. sar.num, sar.den);
  202. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  203. return ret;
  204. } else {
  205. avctx->sample_aspect_ratio = sar;
  206. }
  207. return 0;
  208. }
  209. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  210. enum AVMatrixEncoding matrix_encoding)
  211. {
  212. AVFrameSideData *side_data;
  213. enum AVMatrixEncoding *data;
  214. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  215. if (!side_data)
  216. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  217. sizeof(enum AVMatrixEncoding));
  218. if (!side_data)
  219. return AVERROR(ENOMEM);
  220. data = (enum AVMatrixEncoding*)side_data->data;
  221. *data = matrix_encoding;
  222. return 0;
  223. }
  224. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  225. int linesize_align[AV_NUM_DATA_POINTERS])
  226. {
  227. int i;
  228. int w_align = 1;
  229. int h_align = 1;
  230. AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
  231. if (desc) {
  232. w_align = 1 << desc->log2_chroma_w;
  233. h_align = 1 << desc->log2_chroma_h;
  234. }
  235. switch (s->pix_fmt) {
  236. case AV_PIX_FMT_YUV420P:
  237. case AV_PIX_FMT_YUYV422:
  238. case AV_PIX_FMT_YVYU422:
  239. case AV_PIX_FMT_UYVY422:
  240. case AV_PIX_FMT_YUV422P:
  241. case AV_PIX_FMT_YUV440P:
  242. case AV_PIX_FMT_YUV444P:
  243. case AV_PIX_FMT_GBRP:
  244. case AV_PIX_FMT_GBRAP:
  245. case AV_PIX_FMT_GRAY8:
  246. case AV_PIX_FMT_GRAY16BE:
  247. case AV_PIX_FMT_GRAY16LE:
  248. case AV_PIX_FMT_YUVJ420P:
  249. case AV_PIX_FMT_YUVJ422P:
  250. case AV_PIX_FMT_YUVJ440P:
  251. case AV_PIX_FMT_YUVJ444P:
  252. case AV_PIX_FMT_YUVA420P:
  253. case AV_PIX_FMT_YUVA422P:
  254. case AV_PIX_FMT_YUVA444P:
  255. case AV_PIX_FMT_YUV420P9LE:
  256. case AV_PIX_FMT_YUV420P9BE:
  257. case AV_PIX_FMT_YUV420P10LE:
  258. case AV_PIX_FMT_YUV420P10BE:
  259. case AV_PIX_FMT_YUV420P12LE:
  260. case AV_PIX_FMT_YUV420P12BE:
  261. case AV_PIX_FMT_YUV420P14LE:
  262. case AV_PIX_FMT_YUV420P14BE:
  263. case AV_PIX_FMT_YUV420P16LE:
  264. case AV_PIX_FMT_YUV420P16BE:
  265. case AV_PIX_FMT_YUVA420P9LE:
  266. case AV_PIX_FMT_YUVA420P9BE:
  267. case AV_PIX_FMT_YUVA420P10LE:
  268. case AV_PIX_FMT_YUVA420P10BE:
  269. case AV_PIX_FMT_YUVA420P16LE:
  270. case AV_PIX_FMT_YUVA420P16BE:
  271. case AV_PIX_FMT_YUV422P9LE:
  272. case AV_PIX_FMT_YUV422P9BE:
  273. case AV_PIX_FMT_YUV422P10LE:
  274. case AV_PIX_FMT_YUV422P10BE:
  275. case AV_PIX_FMT_YUV422P12LE:
  276. case AV_PIX_FMT_YUV422P12BE:
  277. case AV_PIX_FMT_YUV422P14LE:
  278. case AV_PIX_FMT_YUV422P14BE:
  279. case AV_PIX_FMT_YUV422P16LE:
  280. case AV_PIX_FMT_YUV422P16BE:
  281. case AV_PIX_FMT_YUVA422P9LE:
  282. case AV_PIX_FMT_YUVA422P9BE:
  283. case AV_PIX_FMT_YUVA422P10LE:
  284. case AV_PIX_FMT_YUVA422P10BE:
  285. case AV_PIX_FMT_YUVA422P16LE:
  286. case AV_PIX_FMT_YUVA422P16BE:
  287. case AV_PIX_FMT_YUV440P10LE:
  288. case AV_PIX_FMT_YUV440P10BE:
  289. case AV_PIX_FMT_YUV440P12LE:
  290. case AV_PIX_FMT_YUV440P12BE:
  291. case AV_PIX_FMT_YUV444P9LE:
  292. case AV_PIX_FMT_YUV444P9BE:
  293. case AV_PIX_FMT_YUV444P10LE:
  294. case AV_PIX_FMT_YUV444P10BE:
  295. case AV_PIX_FMT_YUV444P12LE:
  296. case AV_PIX_FMT_YUV444P12BE:
  297. case AV_PIX_FMT_YUV444P14LE:
  298. case AV_PIX_FMT_YUV444P14BE:
  299. case AV_PIX_FMT_YUV444P16LE:
  300. case AV_PIX_FMT_YUV444P16BE:
  301. case AV_PIX_FMT_YUVA444P9LE:
  302. case AV_PIX_FMT_YUVA444P9BE:
  303. case AV_PIX_FMT_YUVA444P10LE:
  304. case AV_PIX_FMT_YUVA444P10BE:
  305. case AV_PIX_FMT_YUVA444P16LE:
  306. case AV_PIX_FMT_YUVA444P16BE:
  307. case AV_PIX_FMT_GBRP9LE:
  308. case AV_PIX_FMT_GBRP9BE:
  309. case AV_PIX_FMT_GBRP10LE:
  310. case AV_PIX_FMT_GBRP10BE:
  311. case AV_PIX_FMT_GBRP12LE:
  312. case AV_PIX_FMT_GBRP12BE:
  313. case AV_PIX_FMT_GBRP14LE:
  314. case AV_PIX_FMT_GBRP14BE:
  315. case AV_PIX_FMT_GBRP16LE:
  316. case AV_PIX_FMT_GBRP16BE:
  317. case AV_PIX_FMT_GBRAP12LE:
  318. case AV_PIX_FMT_GBRAP12BE:
  319. case AV_PIX_FMT_GBRAP16LE:
  320. case AV_PIX_FMT_GBRAP16BE:
  321. w_align = 16; //FIXME assume 16 pixel per macroblock
  322. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  323. break;
  324. case AV_PIX_FMT_YUV411P:
  325. case AV_PIX_FMT_YUVJ411P:
  326. case AV_PIX_FMT_UYYVYY411:
  327. w_align = 32;
  328. h_align = 16 * 2;
  329. break;
  330. case AV_PIX_FMT_YUV410P:
  331. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  332. w_align = 64;
  333. h_align = 64;
  334. }
  335. break;
  336. case AV_PIX_FMT_RGB555:
  337. if (s->codec_id == AV_CODEC_ID_RPZA) {
  338. w_align = 4;
  339. h_align = 4;
  340. }
  341. break;
  342. case AV_PIX_FMT_PAL8:
  343. case AV_PIX_FMT_BGR8:
  344. case AV_PIX_FMT_RGB8:
  345. if (s->codec_id == AV_CODEC_ID_SMC ||
  346. s->codec_id == AV_CODEC_ID_CINEPAK) {
  347. w_align = 4;
  348. h_align = 4;
  349. }
  350. if (s->codec_id == AV_CODEC_ID_JV) {
  351. w_align = 8;
  352. h_align = 8;
  353. }
  354. break;
  355. case AV_PIX_FMT_BGR24:
  356. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  357. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  358. w_align = 4;
  359. h_align = 4;
  360. }
  361. break;
  362. case AV_PIX_FMT_RGB24:
  363. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  364. w_align = 4;
  365. h_align = 4;
  366. }
  367. break;
  368. default:
  369. break;
  370. }
  371. if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
  372. w_align = FFMAX(w_align, 8);
  373. }
  374. *width = FFALIGN(*width, w_align);
  375. *height = FFALIGN(*height, h_align);
  376. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres) {
  377. // some of the optimized chroma MC reads one line too much
  378. // which is also done in mpeg decoders with lowres > 0
  379. *height += 2;
  380. // H.264 uses edge emulation for out of frame motion vectors, for this
  381. // it requires a temporary area large enough to hold a 21x21 block,
  382. // increasing witdth ensure that the temporary area is large enough,
  383. // the next rounded up width is 32
  384. *width = FFMAX(*width, 32);
  385. }
  386. for (i = 0; i < 4; i++)
  387. linesize_align[i] = STRIDE_ALIGN;
  388. }
  389. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  390. {
  391. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  392. int chroma_shift = desc->log2_chroma_w;
  393. int linesize_align[AV_NUM_DATA_POINTERS];
  394. int align;
  395. avcodec_align_dimensions2(s, width, height, linesize_align);
  396. align = FFMAX(linesize_align[0], linesize_align[3]);
  397. linesize_align[1] <<= chroma_shift;
  398. linesize_align[2] <<= chroma_shift;
  399. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  400. *width = FFALIGN(*width, align);
  401. }
  402. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  403. {
  404. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  405. return AVERROR(EINVAL);
  406. pos--;
  407. *xpos = (pos&1) * 128;
  408. *ypos = ((pos>>1)^(pos<4)) * 128;
  409. return 0;
  410. }
  411. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  412. {
  413. int pos, xout, yout;
  414. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  415. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  416. return pos;
  417. }
  418. return AVCHROMA_LOC_UNSPECIFIED;
  419. }
  420. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  421. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  422. int buf_size, int align)
  423. {
  424. int ch, planar, needed_size, ret = 0;
  425. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  426. frame->nb_samples, sample_fmt,
  427. align);
  428. if (buf_size < needed_size)
  429. return AVERROR(EINVAL);
  430. planar = av_sample_fmt_is_planar(sample_fmt);
  431. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  432. if (!(frame->extended_data = av_mallocz_array(nb_channels,
  433. sizeof(*frame->extended_data))))
  434. return AVERROR(ENOMEM);
  435. } else {
  436. frame->extended_data = frame->data;
  437. }
  438. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  439. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  440. sample_fmt, align)) < 0) {
  441. if (frame->extended_data != frame->data)
  442. av_freep(&frame->extended_data);
  443. return ret;
  444. }
  445. if (frame->extended_data != frame->data) {
  446. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  447. frame->data[ch] = frame->extended_data[ch];
  448. }
  449. return ret;
  450. }
  451. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  452. {
  453. FramePool *pool = avctx->internal->pool;
  454. int i, ret;
  455. switch (avctx->codec_type) {
  456. case AVMEDIA_TYPE_VIDEO: {
  457. uint8_t *data[4];
  458. int linesize[4];
  459. int size[4] = { 0 };
  460. int w = frame->width;
  461. int h = frame->height;
  462. int tmpsize, unaligned;
  463. if (pool->format == frame->format &&
  464. pool->width == frame->width && pool->height == frame->height)
  465. return 0;
  466. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  467. do {
  468. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  469. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  470. ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  471. if (ret < 0)
  472. return ret;
  473. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  474. w += w & ~(w - 1);
  475. unaligned = 0;
  476. for (i = 0; i < 4; i++)
  477. unaligned |= linesize[i] % pool->stride_align[i];
  478. } while (unaligned);
  479. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  480. NULL, linesize);
  481. if (tmpsize < 0)
  482. return -1;
  483. for (i = 0; i < 3 && data[i + 1]; i++)
  484. size[i] = data[i + 1] - data[i];
  485. size[i] = tmpsize - (data[i] - data[0]);
  486. for (i = 0; i < 4; i++) {
  487. av_buffer_pool_uninit(&pool->pools[i]);
  488. pool->linesize[i] = linesize[i];
  489. if (size[i]) {
  490. pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
  491. CONFIG_MEMORY_POISONING ?
  492. NULL :
  493. av_buffer_allocz);
  494. if (!pool->pools[i]) {
  495. ret = AVERROR(ENOMEM);
  496. goto fail;
  497. }
  498. }
  499. }
  500. pool->format = frame->format;
  501. pool->width = frame->width;
  502. pool->height = frame->height;
  503. break;
  504. }
  505. case AVMEDIA_TYPE_AUDIO: {
  506. int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
  507. int planar = av_sample_fmt_is_planar(frame->format);
  508. int planes = planar ? ch : 1;
  509. if (pool->format == frame->format && pool->planes == planes &&
  510. pool->channels == ch && frame->nb_samples == pool->samples)
  511. return 0;
  512. av_buffer_pool_uninit(&pool->pools[0]);
  513. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  514. frame->nb_samples, frame->format, 0);
  515. if (ret < 0)
  516. goto fail;
  517. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  518. if (!pool->pools[0]) {
  519. ret = AVERROR(ENOMEM);
  520. goto fail;
  521. }
  522. pool->format = frame->format;
  523. pool->planes = planes;
  524. pool->channels = ch;
  525. pool->samples = frame->nb_samples;
  526. break;
  527. }
  528. default: av_assert0(0);
  529. }
  530. return 0;
  531. fail:
  532. for (i = 0; i < 4; i++)
  533. av_buffer_pool_uninit(&pool->pools[i]);
  534. pool->format = -1;
  535. pool->planes = pool->channels = pool->samples = 0;
  536. pool->width = pool->height = 0;
  537. return ret;
  538. }
  539. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  540. {
  541. FramePool *pool = avctx->internal->pool;
  542. int planes = pool->planes;
  543. int i;
  544. frame->linesize[0] = pool->linesize[0];
  545. if (planes > AV_NUM_DATA_POINTERS) {
  546. frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
  547. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  548. frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
  549. sizeof(*frame->extended_buf));
  550. if (!frame->extended_data || !frame->extended_buf) {
  551. av_freep(&frame->extended_data);
  552. av_freep(&frame->extended_buf);
  553. return AVERROR(ENOMEM);
  554. }
  555. } else {
  556. frame->extended_data = frame->data;
  557. av_assert0(frame->nb_extended_buf == 0);
  558. }
  559. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  560. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  561. if (!frame->buf[i])
  562. goto fail;
  563. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  564. }
  565. for (i = 0; i < frame->nb_extended_buf; i++) {
  566. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  567. if (!frame->extended_buf[i])
  568. goto fail;
  569. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  570. }
  571. if (avctx->debug & FF_DEBUG_BUFFERS)
  572. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  573. return 0;
  574. fail:
  575. av_frame_unref(frame);
  576. return AVERROR(ENOMEM);
  577. }
  578. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  579. {
  580. FramePool *pool = s->internal->pool;
  581. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  582. int i;
  583. if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
  584. av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
  585. return -1;
  586. }
  587. if (!desc) {
  588. av_log(s, AV_LOG_ERROR,
  589. "Unable to get pixel format descriptor for format %s\n",
  590. av_get_pix_fmt_name(pic->format));
  591. return AVERROR(EINVAL);
  592. }
  593. memset(pic->data, 0, sizeof(pic->data));
  594. pic->extended_data = pic->data;
  595. for (i = 0; i < 4 && pool->pools[i]; i++) {
  596. pic->linesize[i] = pool->linesize[i];
  597. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  598. if (!pic->buf[i])
  599. goto fail;
  600. pic->data[i] = pic->buf[i]->data;
  601. }
  602. for (; i < AV_NUM_DATA_POINTERS; i++) {
  603. pic->data[i] = NULL;
  604. pic->linesize[i] = 0;
  605. }
  606. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  607. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  608. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], pic->format);
  609. if (s->debug & FF_DEBUG_BUFFERS)
  610. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  611. return 0;
  612. fail:
  613. av_frame_unref(pic);
  614. return AVERROR(ENOMEM);
  615. }
  616. void ff_color_frame(AVFrame *frame, const int c[4])
  617. {
  618. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  619. int p, y, x;
  620. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  621. for (p = 0; p<desc->nb_components; p++) {
  622. uint8_t *dst = frame->data[p];
  623. int is_chroma = p == 1 || p == 2;
  624. int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  625. int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  626. for (y = 0; y < height; y++) {
  627. if (desc->comp[0].depth >= 9) {
  628. for (x = 0; x<bytes; x++)
  629. ((uint16_t*)dst)[x] = c[p];
  630. }else
  631. memset(dst, c[p], bytes);
  632. dst += frame->linesize[p];
  633. }
  634. }
  635. }
  636. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  637. {
  638. int ret;
  639. if (avctx->hw_frames_ctx)
  640. return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
  641. if ((ret = update_frame_pool(avctx, frame)) < 0)
  642. return ret;
  643. switch (avctx->codec_type) {
  644. case AVMEDIA_TYPE_VIDEO:
  645. return video_get_buffer(avctx, frame);
  646. case AVMEDIA_TYPE_AUDIO:
  647. return audio_get_buffer(avctx, frame);
  648. default:
  649. return -1;
  650. }
  651. }
  652. static int add_metadata_from_side_data(AVPacket *avpkt, AVFrame *frame)
  653. {
  654. int size;
  655. const uint8_t *side_metadata;
  656. AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
  657. side_metadata = av_packet_get_side_data(avpkt,
  658. AV_PKT_DATA_STRINGS_METADATA, &size);
  659. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  660. }
  661. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  662. {
  663. AVPacket *pkt = avctx->internal->pkt;
  664. int i;
  665. static const struct {
  666. enum AVPacketSideDataType packet;
  667. enum AVFrameSideDataType frame;
  668. } sd[] = {
  669. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  670. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  671. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  672. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  673. { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
  674. };
  675. if (pkt) {
  676. frame->pkt_pts = pkt->pts;
  677. av_frame_set_pkt_pos (frame, pkt->pos);
  678. av_frame_set_pkt_duration(frame, pkt->duration);
  679. av_frame_set_pkt_size (frame, pkt->size);
  680. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  681. int size;
  682. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  683. if (packet_sd) {
  684. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  685. sd[i].frame,
  686. size);
  687. if (!frame_sd)
  688. return AVERROR(ENOMEM);
  689. memcpy(frame_sd->data, packet_sd, size);
  690. }
  691. }
  692. add_metadata_from_side_data(pkt, frame);
  693. if (pkt->flags & AV_PKT_FLAG_DISCARD) {
  694. frame->flags |= AV_FRAME_FLAG_DISCARD;
  695. } else {
  696. frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
  697. }
  698. } else {
  699. frame->pkt_pts = AV_NOPTS_VALUE;
  700. av_frame_set_pkt_pos (frame, -1);
  701. av_frame_set_pkt_duration(frame, 0);
  702. av_frame_set_pkt_size (frame, -1);
  703. }
  704. frame->reordered_opaque = avctx->reordered_opaque;
  705. if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
  706. frame->color_primaries = avctx->color_primaries;
  707. if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
  708. frame->color_trc = avctx->color_trc;
  709. if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
  710. av_frame_set_colorspace(frame, avctx->colorspace);
  711. if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
  712. av_frame_set_color_range(frame, avctx->color_range);
  713. if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
  714. frame->chroma_location = avctx->chroma_sample_location;
  715. switch (avctx->codec->type) {
  716. case AVMEDIA_TYPE_VIDEO:
  717. frame->format = avctx->pix_fmt;
  718. if (!frame->sample_aspect_ratio.num)
  719. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  720. if (frame->width && frame->height &&
  721. av_image_check_sar(frame->width, frame->height,
  722. frame->sample_aspect_ratio) < 0) {
  723. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  724. frame->sample_aspect_ratio.num,
  725. frame->sample_aspect_ratio.den);
  726. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  727. }
  728. break;
  729. case AVMEDIA_TYPE_AUDIO:
  730. if (!frame->sample_rate)
  731. frame->sample_rate = avctx->sample_rate;
  732. if (frame->format < 0)
  733. frame->format = avctx->sample_fmt;
  734. if (!frame->channel_layout) {
  735. if (avctx->channel_layout) {
  736. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  737. avctx->channels) {
  738. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  739. "configuration.\n");
  740. return AVERROR(EINVAL);
  741. }
  742. frame->channel_layout = avctx->channel_layout;
  743. } else {
  744. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  745. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  746. avctx->channels);
  747. return AVERROR(ENOSYS);
  748. }
  749. }
  750. }
  751. av_frame_set_channels(frame, avctx->channels);
  752. break;
  753. }
  754. return 0;
  755. }
  756. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  757. {
  758. return ff_init_buffer_info(avctx, frame);
  759. }
  760. static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
  761. {
  762. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  763. int i;
  764. int num_planes = av_pix_fmt_count_planes(frame->format);
  765. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  766. int flags = desc ? desc->flags : 0;
  767. if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
  768. num_planes = 2;
  769. for (i = 0; i < num_planes; i++) {
  770. av_assert0(frame->data[i]);
  771. }
  772. // For now do not enforce anything for palette of pseudopal formats
  773. if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PSEUDOPAL))
  774. num_planes = 2;
  775. // For formats without data like hwaccel allow unused pointers to be non-NULL.
  776. for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
  777. if (frame->data[i])
  778. av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
  779. frame->data[i] = NULL;
  780. }
  781. }
  782. }
  783. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  784. {
  785. const AVHWAccel *hwaccel = avctx->hwaccel;
  786. int override_dimensions = 1;
  787. int ret;
  788. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  789. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  790. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  791. return AVERROR(EINVAL);
  792. }
  793. if (frame->width <= 0 || frame->height <= 0) {
  794. frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
  795. frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  796. override_dimensions = 0;
  797. }
  798. if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
  799. av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
  800. return AVERROR(EINVAL);
  801. }
  802. }
  803. ret = ff_decode_frame_props(avctx, frame);
  804. if (ret < 0)
  805. return ret;
  806. if (hwaccel) {
  807. if (hwaccel->alloc_frame) {
  808. ret = hwaccel->alloc_frame(avctx, frame);
  809. goto end;
  810. }
  811. } else
  812. avctx->sw_pix_fmt = avctx->pix_fmt;
  813. ret = avctx->get_buffer2(avctx, frame, flags);
  814. if (ret >= 0)
  815. validate_avframe_allocation(avctx, frame);
  816. end:
  817. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  818. frame->width = avctx->width;
  819. frame->height = avctx->height;
  820. }
  821. return ret;
  822. }
  823. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  824. {
  825. int ret = get_buffer_internal(avctx, frame, flags);
  826. if (ret < 0) {
  827. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  828. frame->width = frame->height = 0;
  829. }
  830. return ret;
  831. }
  832. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  833. {
  834. AVFrame *tmp;
  835. int ret;
  836. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  837. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  838. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  839. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  840. av_frame_unref(frame);
  841. }
  842. ff_init_buffer_info(avctx, frame);
  843. if (!frame->data[0])
  844. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  845. if (av_frame_is_writable(frame))
  846. return ff_decode_frame_props(avctx, frame);
  847. tmp = av_frame_alloc();
  848. if (!tmp)
  849. return AVERROR(ENOMEM);
  850. av_frame_move_ref(tmp, frame);
  851. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  852. if (ret < 0) {
  853. av_frame_free(&tmp);
  854. return ret;
  855. }
  856. av_frame_copy(frame, tmp);
  857. av_frame_free(&tmp);
  858. return 0;
  859. }
  860. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  861. {
  862. int ret = reget_buffer_internal(avctx, frame);
  863. if (ret < 0)
  864. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  865. return ret;
  866. }
  867. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  868. {
  869. int i;
  870. for (i = 0; i < count; i++) {
  871. int r = func(c, (char *)arg + i * size);
  872. if (ret)
  873. ret[i] = r;
  874. }
  875. return 0;
  876. }
  877. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  878. {
  879. int i;
  880. for (i = 0; i < count; i++) {
  881. int r = func(c, arg, i, 0);
  882. if (ret)
  883. ret[i] = r;
  884. }
  885. return 0;
  886. }
  887. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  888. unsigned int fourcc)
  889. {
  890. while (tags->pix_fmt >= 0) {
  891. if (tags->fourcc == fourcc)
  892. return tags->pix_fmt;
  893. tags++;
  894. }
  895. return AV_PIX_FMT_NONE;
  896. }
  897. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  898. {
  899. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  900. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  901. }
  902. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  903. {
  904. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  905. ++fmt;
  906. return fmt[0];
  907. }
  908. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  909. enum AVPixelFormat pix_fmt)
  910. {
  911. AVHWAccel *hwaccel = NULL;
  912. while ((hwaccel = av_hwaccel_next(hwaccel)))
  913. if (hwaccel->id == codec_id
  914. && hwaccel->pix_fmt == pix_fmt)
  915. return hwaccel;
  916. return NULL;
  917. }
  918. static int setup_hwaccel(AVCodecContext *avctx,
  919. const enum AVPixelFormat fmt,
  920. const char *name)
  921. {
  922. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  923. int ret = 0;
  924. if (avctx->active_thread_type & FF_THREAD_FRAME) {
  925. av_log(avctx, AV_LOG_WARNING,
  926. "Hardware accelerated decoding with frame threading is known to be unstable and its use is discouraged.\n");
  927. }
  928. if (!hwa) {
  929. av_log(avctx, AV_LOG_ERROR,
  930. "Could not find an AVHWAccel for the pixel format: %s",
  931. name);
  932. return AVERROR(ENOENT);
  933. }
  934. if (hwa->capabilities & HWACCEL_CODEC_CAP_EXPERIMENTAL &&
  935. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  936. av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
  937. hwa->name);
  938. return AVERROR_PATCHWELCOME;
  939. }
  940. if (hwa->priv_data_size) {
  941. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  942. if (!avctx->internal->hwaccel_priv_data)
  943. return AVERROR(ENOMEM);
  944. }
  945. if (hwa->init) {
  946. ret = hwa->init(avctx);
  947. if (ret < 0) {
  948. av_freep(&avctx->internal->hwaccel_priv_data);
  949. return ret;
  950. }
  951. }
  952. avctx->hwaccel = hwa;
  953. return 0;
  954. }
  955. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  956. {
  957. const AVPixFmtDescriptor *desc;
  958. enum AVPixelFormat *choices;
  959. enum AVPixelFormat ret;
  960. unsigned n = 0;
  961. while (fmt[n] != AV_PIX_FMT_NONE)
  962. ++n;
  963. av_assert0(n >= 1);
  964. avctx->sw_pix_fmt = fmt[n - 1];
  965. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  966. choices = av_malloc_array(n + 1, sizeof(*choices));
  967. if (!choices)
  968. return AV_PIX_FMT_NONE;
  969. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  970. for (;;) {
  971. if (avctx->hwaccel && avctx->hwaccel->uninit)
  972. avctx->hwaccel->uninit(avctx);
  973. av_freep(&avctx->internal->hwaccel_priv_data);
  974. avctx->hwaccel = NULL;
  975. av_buffer_unref(&avctx->hw_frames_ctx);
  976. ret = avctx->get_format(avctx, choices);
  977. desc = av_pix_fmt_desc_get(ret);
  978. if (!desc) {
  979. ret = AV_PIX_FMT_NONE;
  980. break;
  981. }
  982. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  983. break;
  984. #if FF_API_CAP_VDPAU
  985. if (avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU)
  986. break;
  987. #endif
  988. if (avctx->hw_frames_ctx) {
  989. AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  990. if (hw_frames_ctx->format != ret) {
  991. av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
  992. "does not match the format of provided AVHWFramesContext\n");
  993. ret = AV_PIX_FMT_NONE;
  994. break;
  995. }
  996. }
  997. if (!setup_hwaccel(avctx, ret, desc->name))
  998. break;
  999. /* Remove failed hwaccel from choices */
  1000. for (n = 0; choices[n] != ret; n++)
  1001. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  1002. do
  1003. choices[n] = choices[n + 1];
  1004. while (choices[n++] != AV_PIX_FMT_NONE);
  1005. }
  1006. av_freep(&choices);
  1007. return ret;
  1008. }
  1009. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  1010. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  1011. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  1012. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  1013. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  1014. unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
  1015. {
  1016. return codec->properties;
  1017. }
  1018. int av_codec_get_max_lowres(const AVCodec *codec)
  1019. {
  1020. return codec->max_lowres;
  1021. }
  1022. int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
  1023. return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
  1024. }
  1025. static void get_subtitle_defaults(AVSubtitle *sub)
  1026. {
  1027. memset(sub, 0, sizeof(*sub));
  1028. sub->pts = AV_NOPTS_VALUE;
  1029. }
  1030. static int64_t get_bit_rate(AVCodecContext *ctx)
  1031. {
  1032. int64_t bit_rate;
  1033. int bits_per_sample;
  1034. switch (ctx->codec_type) {
  1035. case AVMEDIA_TYPE_VIDEO:
  1036. case AVMEDIA_TYPE_DATA:
  1037. case AVMEDIA_TYPE_SUBTITLE:
  1038. case AVMEDIA_TYPE_ATTACHMENT:
  1039. bit_rate = ctx->bit_rate;
  1040. break;
  1041. case AVMEDIA_TYPE_AUDIO:
  1042. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1043. bit_rate = bits_per_sample ? ctx->sample_rate * (int64_t)ctx->channels * bits_per_sample : ctx->bit_rate;
  1044. break;
  1045. default:
  1046. bit_rate = 0;
  1047. break;
  1048. }
  1049. return bit_rate;
  1050. }
  1051. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1052. {
  1053. int ret = 0;
  1054. ff_unlock_avcodec(codec);
  1055. ret = avcodec_open2(avctx, codec, options);
  1056. ff_lock_avcodec(avctx, codec);
  1057. return ret;
  1058. }
  1059. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1060. {
  1061. int ret = 0;
  1062. AVDictionary *tmp = NULL;
  1063. const AVPixFmtDescriptor *pixdesc;
  1064. if (avcodec_is_open(avctx))
  1065. return 0;
  1066. if ((!codec && !avctx->codec)) {
  1067. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  1068. return AVERROR(EINVAL);
  1069. }
  1070. if ((codec && avctx->codec && codec != avctx->codec)) {
  1071. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  1072. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  1073. return AVERROR(EINVAL);
  1074. }
  1075. if (!codec)
  1076. codec = avctx->codec;
  1077. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  1078. return AVERROR(EINVAL);
  1079. if (options)
  1080. av_dict_copy(&tmp, *options, 0);
  1081. ret = ff_lock_avcodec(avctx, codec);
  1082. if (ret < 0)
  1083. return ret;
  1084. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  1085. if (!avctx->internal) {
  1086. ret = AVERROR(ENOMEM);
  1087. goto end;
  1088. }
  1089. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  1090. if (!avctx->internal->pool) {
  1091. ret = AVERROR(ENOMEM);
  1092. goto free_and_end;
  1093. }
  1094. avctx->internal->to_free = av_frame_alloc();
  1095. if (!avctx->internal->to_free) {
  1096. ret = AVERROR(ENOMEM);
  1097. goto free_and_end;
  1098. }
  1099. avctx->internal->buffer_frame = av_frame_alloc();
  1100. if (!avctx->internal->buffer_frame) {
  1101. ret = AVERROR(ENOMEM);
  1102. goto free_and_end;
  1103. }
  1104. avctx->internal->buffer_pkt = av_packet_alloc();
  1105. if (!avctx->internal->buffer_pkt) {
  1106. ret = AVERROR(ENOMEM);
  1107. goto free_and_end;
  1108. }
  1109. if (codec->priv_data_size > 0) {
  1110. if (!avctx->priv_data) {
  1111. avctx->priv_data = av_mallocz(codec->priv_data_size);
  1112. if (!avctx->priv_data) {
  1113. ret = AVERROR(ENOMEM);
  1114. goto end;
  1115. }
  1116. if (codec->priv_class) {
  1117. *(const AVClass **)avctx->priv_data = codec->priv_class;
  1118. av_opt_set_defaults(avctx->priv_data);
  1119. }
  1120. }
  1121. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  1122. goto free_and_end;
  1123. } else {
  1124. avctx->priv_data = NULL;
  1125. }
  1126. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  1127. goto free_and_end;
  1128. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  1129. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
  1130. ret = AVERROR(EINVAL);
  1131. goto free_and_end;
  1132. }
  1133. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  1134. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1135. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  1136. if (avctx->coded_width && avctx->coded_height)
  1137. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1138. else if (avctx->width && avctx->height)
  1139. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1140. if (ret < 0)
  1141. goto free_and_end;
  1142. }
  1143. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1144. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1145. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  1146. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1147. ff_set_dimensions(avctx, 0, 0);
  1148. }
  1149. if (avctx->width > 0 && avctx->height > 0) {
  1150. if (av_image_check_sar(avctx->width, avctx->height,
  1151. avctx->sample_aspect_ratio) < 0) {
  1152. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1153. avctx->sample_aspect_ratio.num,
  1154. avctx->sample_aspect_ratio.den);
  1155. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  1156. }
  1157. }
  1158. /* if the decoder init function was already called previously,
  1159. * free the already allocated subtitle_header before overwriting it */
  1160. if (av_codec_is_decoder(codec))
  1161. av_freep(&avctx->subtitle_header);
  1162. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1163. ret = AVERROR(EINVAL);
  1164. goto free_and_end;
  1165. }
  1166. avctx->codec = codec;
  1167. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1168. avctx->codec_id == AV_CODEC_ID_NONE) {
  1169. avctx->codec_type = codec->type;
  1170. avctx->codec_id = codec->id;
  1171. }
  1172. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1173. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1174. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1175. ret = AVERROR(EINVAL);
  1176. goto free_and_end;
  1177. }
  1178. avctx->frame_number = 0;
  1179. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1180. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  1181. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1182. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1183. AVCodec *codec2;
  1184. av_log(avctx, AV_LOG_ERROR,
  1185. "The %s '%s' is experimental but experimental codecs are not enabled, "
  1186. "add '-strict %d' if you want to use it.\n",
  1187. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1188. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1189. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  1190. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1191. codec_string, codec2->name);
  1192. ret = AVERROR_EXPERIMENTAL;
  1193. goto free_and_end;
  1194. }
  1195. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1196. (!avctx->time_base.num || !avctx->time_base.den)) {
  1197. avctx->time_base.num = 1;
  1198. avctx->time_base.den = avctx->sample_rate;
  1199. }
  1200. if (!HAVE_THREADS)
  1201. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1202. if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
  1203. ff_unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
  1204. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1205. ff_lock_avcodec(avctx, codec);
  1206. if (ret < 0)
  1207. goto free_and_end;
  1208. }
  1209. if (HAVE_THREADS
  1210. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1211. ret = ff_thread_init(avctx);
  1212. if (ret < 0) {
  1213. goto free_and_end;
  1214. }
  1215. }
  1216. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  1217. avctx->thread_count = 1;
  1218. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1219. av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
  1220. avctx->codec->max_lowres);
  1221. avctx->lowres = avctx->codec->max_lowres;
  1222. }
  1223. #if FF_API_VISMV
  1224. if (avctx->debug_mv)
  1225. av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
  1226. "see the codecview filter instead.\n");
  1227. #endif
  1228. if (av_codec_is_encoder(avctx->codec)) {
  1229. int i;
  1230. #if FF_API_CODED_FRAME
  1231. FF_DISABLE_DEPRECATION_WARNINGS
  1232. avctx->coded_frame = av_frame_alloc();
  1233. if (!avctx->coded_frame) {
  1234. ret = AVERROR(ENOMEM);
  1235. goto free_and_end;
  1236. }
  1237. FF_ENABLE_DEPRECATION_WARNINGS
  1238. #endif
  1239. if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
  1240. av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
  1241. ret = AVERROR(EINVAL);
  1242. goto free_and_end;
  1243. }
  1244. if (avctx->codec->sample_fmts) {
  1245. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1246. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1247. break;
  1248. if (avctx->channels == 1 &&
  1249. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1250. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1251. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1252. break;
  1253. }
  1254. }
  1255. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1256. char buf[128];
  1257. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1258. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1259. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1260. ret = AVERROR(EINVAL);
  1261. goto free_and_end;
  1262. }
  1263. }
  1264. if (avctx->codec->pix_fmts) {
  1265. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1266. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1267. break;
  1268. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1269. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1270. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1271. char buf[128];
  1272. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1273. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1274. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1275. ret = AVERROR(EINVAL);
  1276. goto free_and_end;
  1277. }
  1278. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  1279. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
  1280. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  1281. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  1282. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  1283. avctx->color_range = AVCOL_RANGE_JPEG;
  1284. }
  1285. if (avctx->codec->supported_samplerates) {
  1286. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1287. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1288. break;
  1289. if (avctx->codec->supported_samplerates[i] == 0) {
  1290. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1291. avctx->sample_rate);
  1292. ret = AVERROR(EINVAL);
  1293. goto free_and_end;
  1294. }
  1295. }
  1296. if (avctx->sample_rate < 0) {
  1297. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1298. avctx->sample_rate);
  1299. ret = AVERROR(EINVAL);
  1300. goto free_and_end;
  1301. }
  1302. if (avctx->codec->channel_layouts) {
  1303. if (!avctx->channel_layout) {
  1304. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1305. } else {
  1306. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1307. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1308. break;
  1309. if (avctx->codec->channel_layouts[i] == 0) {
  1310. char buf[512];
  1311. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1312. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1313. ret = AVERROR(EINVAL);
  1314. goto free_and_end;
  1315. }
  1316. }
  1317. }
  1318. if (avctx->channel_layout && avctx->channels) {
  1319. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1320. if (channels != avctx->channels) {
  1321. char buf[512];
  1322. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1323. av_log(avctx, AV_LOG_ERROR,
  1324. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1325. buf, channels, avctx->channels);
  1326. ret = AVERROR(EINVAL);
  1327. goto free_and_end;
  1328. }
  1329. } else if (avctx->channel_layout) {
  1330. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1331. }
  1332. if (avctx->channels < 0) {
  1333. av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
  1334. avctx->channels);
  1335. ret = AVERROR(EINVAL);
  1336. goto free_and_end;
  1337. }
  1338. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1339. pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1340. if ( avctx->bits_per_raw_sample < 0
  1341. || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
  1342. av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
  1343. avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
  1344. avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
  1345. }
  1346. if (avctx->width <= 0 || avctx->height <= 0) {
  1347. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1348. ret = AVERROR(EINVAL);
  1349. goto free_and_end;
  1350. }
  1351. }
  1352. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1353. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1354. av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", (int64_t)avctx->bit_rate, (int64_t)avctx->bit_rate);
  1355. }
  1356. if (!avctx->rc_initial_buffer_occupancy)
  1357. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1358. if (avctx->ticks_per_frame && avctx->time_base.num &&
  1359. avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
  1360. av_log(avctx, AV_LOG_ERROR,
  1361. "ticks_per_frame %d too large for the timebase %d/%d.",
  1362. avctx->ticks_per_frame,
  1363. avctx->time_base.num,
  1364. avctx->time_base.den);
  1365. goto free_and_end;
  1366. }
  1367. if (avctx->hw_frames_ctx) {
  1368. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  1369. if (frames_ctx->format != avctx->pix_fmt) {
  1370. av_log(avctx, AV_LOG_ERROR,
  1371. "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
  1372. ret = AVERROR(EINVAL);
  1373. goto free_and_end;
  1374. }
  1375. }
  1376. }
  1377. avctx->pts_correction_num_faulty_pts =
  1378. avctx->pts_correction_num_faulty_dts = 0;
  1379. avctx->pts_correction_last_pts =
  1380. avctx->pts_correction_last_dts = INT64_MIN;
  1381. if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
  1382. && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
  1383. av_log(avctx, AV_LOG_WARNING,
  1384. "gray decoding requested but not enabled at configuration time\n");
  1385. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1386. || avctx->internal->frame_thread_encoder)) {
  1387. ret = avctx->codec->init(avctx);
  1388. if (ret < 0) {
  1389. goto free_and_end;
  1390. }
  1391. }
  1392. ret=0;
  1393. #if FF_API_AUDIOENC_DELAY
  1394. if (av_codec_is_encoder(avctx->codec))
  1395. avctx->delay = avctx->initial_padding;
  1396. #endif
  1397. if (av_codec_is_decoder(avctx->codec)) {
  1398. if (!avctx->bit_rate)
  1399. avctx->bit_rate = get_bit_rate(avctx);
  1400. /* validate channel layout from the decoder */
  1401. if (avctx->channel_layout) {
  1402. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1403. if (!avctx->channels)
  1404. avctx->channels = channels;
  1405. else if (channels != avctx->channels) {
  1406. char buf[512];
  1407. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1408. av_log(avctx, AV_LOG_WARNING,
  1409. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1410. "ignoring specified channel layout\n",
  1411. buf, channels, avctx->channels);
  1412. avctx->channel_layout = 0;
  1413. }
  1414. }
  1415. if (avctx->channels && avctx->channels < 0 ||
  1416. avctx->channels > FF_SANE_NB_CHANNELS) {
  1417. ret = AVERROR(EINVAL);
  1418. goto free_and_end;
  1419. }
  1420. if (avctx->sub_charenc) {
  1421. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1422. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1423. "supported with subtitles codecs\n");
  1424. ret = AVERROR(EINVAL);
  1425. goto free_and_end;
  1426. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1427. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1428. "subtitles character encoding will be ignored\n",
  1429. avctx->codec_descriptor->name);
  1430. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1431. } else {
  1432. /* input character encoding is set for a text based subtitle
  1433. * codec at this point */
  1434. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1435. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1436. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1437. #if CONFIG_ICONV
  1438. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1439. if (cd == (iconv_t)-1) {
  1440. ret = AVERROR(errno);
  1441. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1442. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1443. goto free_and_end;
  1444. }
  1445. iconv_close(cd);
  1446. #else
  1447. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1448. "conversion needs a libavcodec built with iconv support "
  1449. "for this codec\n");
  1450. ret = AVERROR(ENOSYS);
  1451. goto free_and_end;
  1452. #endif
  1453. }
  1454. }
  1455. }
  1456. #if FF_API_AVCTX_TIMEBASE
  1457. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1458. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  1459. #endif
  1460. }
  1461. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  1462. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  1463. }
  1464. end:
  1465. ff_unlock_avcodec(codec);
  1466. if (options) {
  1467. av_dict_free(options);
  1468. *options = tmp;
  1469. }
  1470. return ret;
  1471. free_and_end:
  1472. if (avctx->codec &&
  1473. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
  1474. avctx->codec->close(avctx);
  1475. if (codec->priv_class && codec->priv_data_size)
  1476. av_opt_free(avctx->priv_data);
  1477. av_opt_free(avctx);
  1478. #if FF_API_CODED_FRAME
  1479. FF_DISABLE_DEPRECATION_WARNINGS
  1480. av_frame_free(&avctx->coded_frame);
  1481. FF_ENABLE_DEPRECATION_WARNINGS
  1482. #endif
  1483. av_dict_free(&tmp);
  1484. av_freep(&avctx->priv_data);
  1485. if (avctx->internal) {
  1486. av_packet_free(&avctx->internal->buffer_pkt);
  1487. av_frame_free(&avctx->internal->buffer_frame);
  1488. av_frame_free(&avctx->internal->to_free);
  1489. av_freep(&avctx->internal->pool);
  1490. }
  1491. av_freep(&avctx->internal);
  1492. avctx->codec = NULL;
  1493. goto end;
  1494. }
  1495. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
  1496. {
  1497. if (avpkt->size < 0) {
  1498. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1499. return AVERROR(EINVAL);
  1500. }
  1501. if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  1502. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1503. size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
  1504. return AVERROR(EINVAL);
  1505. }
  1506. if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
  1507. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1508. if (!avpkt->data || avpkt->size < size) {
  1509. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1510. avpkt->data = avctx->internal->byte_buffer;
  1511. avpkt->size = avctx->internal->byte_buffer_size;
  1512. }
  1513. }
  1514. if (avpkt->data) {
  1515. AVBufferRef *buf = avpkt->buf;
  1516. if (avpkt->size < size) {
  1517. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1518. return AVERROR(EINVAL);
  1519. }
  1520. av_init_packet(avpkt);
  1521. avpkt->buf = buf;
  1522. avpkt->size = size;
  1523. return 0;
  1524. } else {
  1525. int ret = av_new_packet(avpkt, size);
  1526. if (ret < 0)
  1527. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1528. return ret;
  1529. }
  1530. }
  1531. int ff_alloc_packet(AVPacket *avpkt, int size)
  1532. {
  1533. return ff_alloc_packet2(NULL, avpkt, size, 0);
  1534. }
  1535. /**
  1536. * Pad last frame with silence.
  1537. */
  1538. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1539. {
  1540. AVFrame *frame = NULL;
  1541. int ret;
  1542. if (!(frame = av_frame_alloc()))
  1543. return AVERROR(ENOMEM);
  1544. frame->format = src->format;
  1545. frame->channel_layout = src->channel_layout;
  1546. av_frame_set_channels(frame, av_frame_get_channels(src));
  1547. frame->nb_samples = s->frame_size;
  1548. ret = av_frame_get_buffer(frame, 32);
  1549. if (ret < 0)
  1550. goto fail;
  1551. ret = av_frame_copy_props(frame, src);
  1552. if (ret < 0)
  1553. goto fail;
  1554. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1555. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1556. goto fail;
  1557. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1558. frame->nb_samples - src->nb_samples,
  1559. s->channels, s->sample_fmt)) < 0)
  1560. goto fail;
  1561. *dst = frame;
  1562. return 0;
  1563. fail:
  1564. av_frame_free(&frame);
  1565. return ret;
  1566. }
  1567. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1568. AVPacket *avpkt,
  1569. const AVFrame *frame,
  1570. int *got_packet_ptr)
  1571. {
  1572. AVFrame *extended_frame = NULL;
  1573. AVFrame *padded_frame = NULL;
  1574. int ret;
  1575. AVPacket user_pkt = *avpkt;
  1576. int needs_realloc = !user_pkt.data;
  1577. *got_packet_ptr = 0;
  1578. if (!avctx->codec->encode2) {
  1579. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  1580. return AVERROR(ENOSYS);
  1581. }
  1582. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1583. av_packet_unref(avpkt);
  1584. av_init_packet(avpkt);
  1585. return 0;
  1586. }
  1587. /* ensure that extended_data is properly set */
  1588. if (frame && !frame->extended_data) {
  1589. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1590. avctx->channels > AV_NUM_DATA_POINTERS) {
  1591. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1592. "with more than %d channels, but extended_data is not set.\n",
  1593. AV_NUM_DATA_POINTERS);
  1594. return AVERROR(EINVAL);
  1595. }
  1596. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1597. extended_frame = av_frame_alloc();
  1598. if (!extended_frame)
  1599. return AVERROR(ENOMEM);
  1600. memcpy(extended_frame, frame, sizeof(AVFrame));
  1601. extended_frame->extended_data = extended_frame->data;
  1602. frame = extended_frame;
  1603. }
  1604. /* extract audio service type metadata */
  1605. if (frame) {
  1606. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  1607. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  1608. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  1609. }
  1610. /* check for valid frame size */
  1611. if (frame) {
  1612. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  1613. if (frame->nb_samples > avctx->frame_size) {
  1614. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1615. ret = AVERROR(EINVAL);
  1616. goto end;
  1617. }
  1618. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1619. if (frame->nb_samples < avctx->frame_size &&
  1620. !avctx->internal->last_audio_frame) {
  1621. ret = pad_last_frame(avctx, &padded_frame, frame);
  1622. if (ret < 0)
  1623. goto end;
  1624. frame = padded_frame;
  1625. avctx->internal->last_audio_frame = 1;
  1626. }
  1627. if (frame->nb_samples != avctx->frame_size) {
  1628. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1629. ret = AVERROR(EINVAL);
  1630. goto end;
  1631. }
  1632. }
  1633. }
  1634. av_assert0(avctx->codec->encode2);
  1635. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1636. if (!ret) {
  1637. if (*got_packet_ptr) {
  1638. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  1639. if (avpkt->pts == AV_NOPTS_VALUE)
  1640. avpkt->pts = frame->pts;
  1641. if (!avpkt->duration)
  1642. avpkt->duration = ff_samples_to_time_base(avctx,
  1643. frame->nb_samples);
  1644. }
  1645. avpkt->dts = avpkt->pts;
  1646. } else {
  1647. avpkt->size = 0;
  1648. }
  1649. }
  1650. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1651. needs_realloc = 0;
  1652. if (user_pkt.data) {
  1653. if (user_pkt.size >= avpkt->size) {
  1654. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1655. } else {
  1656. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1657. avpkt->size = user_pkt.size;
  1658. ret = -1;
  1659. }
  1660. avpkt->buf = user_pkt.buf;
  1661. avpkt->data = user_pkt.data;
  1662. } else {
  1663. if (av_dup_packet(avpkt) < 0) {
  1664. ret = AVERROR(ENOMEM);
  1665. }
  1666. }
  1667. }
  1668. if (!ret) {
  1669. if (needs_realloc && avpkt->data) {
  1670. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  1671. if (ret >= 0)
  1672. avpkt->data = avpkt->buf->data;
  1673. }
  1674. avctx->frame_number++;
  1675. }
  1676. if (ret < 0 || !*got_packet_ptr) {
  1677. av_packet_unref(avpkt);
  1678. av_init_packet(avpkt);
  1679. goto end;
  1680. }
  1681. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1682. * this needs to be moved to the encoders, but for now we can do it
  1683. * here to simplify things */
  1684. avpkt->flags |= AV_PKT_FLAG_KEY;
  1685. end:
  1686. av_frame_free(&padded_frame);
  1687. av_free(extended_frame);
  1688. #if FF_API_AUDIOENC_DELAY
  1689. avctx->delay = avctx->initial_padding;
  1690. #endif
  1691. return ret;
  1692. }
  1693. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1694. AVPacket *avpkt,
  1695. const AVFrame *frame,
  1696. int *got_packet_ptr)
  1697. {
  1698. int ret;
  1699. AVPacket user_pkt = *avpkt;
  1700. int needs_realloc = !user_pkt.data;
  1701. *got_packet_ptr = 0;
  1702. if (!avctx->codec->encode2) {
  1703. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  1704. return AVERROR(ENOSYS);
  1705. }
  1706. if(CONFIG_FRAME_THREAD_ENCODER &&
  1707. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1708. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1709. if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  1710. avctx->stats_out[0] = '\0';
  1711. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1712. av_packet_unref(avpkt);
  1713. av_init_packet(avpkt);
  1714. avpkt->size = 0;
  1715. return 0;
  1716. }
  1717. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1718. return AVERROR(EINVAL);
  1719. if (frame && frame->format == AV_PIX_FMT_NONE)
  1720. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  1721. if (frame && (frame->width == 0 || frame->height == 0))
  1722. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  1723. av_assert0(avctx->codec->encode2);
  1724. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1725. av_assert0(ret <= 0);
  1726. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1727. needs_realloc = 0;
  1728. if (user_pkt.data) {
  1729. if (user_pkt.size >= avpkt->size) {
  1730. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1731. } else {
  1732. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1733. avpkt->size = user_pkt.size;
  1734. ret = -1;
  1735. }
  1736. avpkt->buf = user_pkt.buf;
  1737. avpkt->data = user_pkt.data;
  1738. } else {
  1739. if (av_dup_packet(avpkt) < 0) {
  1740. ret = AVERROR(ENOMEM);
  1741. }
  1742. }
  1743. }
  1744. if (!ret) {
  1745. if (!*got_packet_ptr)
  1746. avpkt->size = 0;
  1747. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1748. avpkt->pts = avpkt->dts = frame->pts;
  1749. if (needs_realloc && avpkt->data) {
  1750. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  1751. if (ret >= 0)
  1752. avpkt->data = avpkt->buf->data;
  1753. }
  1754. avctx->frame_number++;
  1755. }
  1756. if (ret < 0 || !*got_packet_ptr)
  1757. av_packet_unref(avpkt);
  1758. emms_c();
  1759. return ret;
  1760. }
  1761. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1762. const AVSubtitle *sub)
  1763. {
  1764. int ret;
  1765. if (sub->start_display_time) {
  1766. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1767. return -1;
  1768. }
  1769. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1770. avctx->frame_number++;
  1771. return ret;
  1772. }
  1773. /**
  1774. * Attempt to guess proper monotonic timestamps for decoded video frames
  1775. * which might have incorrect times. Input timestamps may wrap around, in
  1776. * which case the output will as well.
  1777. *
  1778. * @param pts the pts field of the decoded AVPacket, as passed through
  1779. * AVFrame.pkt_pts
  1780. * @param dts the dts field of the decoded AVPacket
  1781. * @return one of the input values, may be AV_NOPTS_VALUE
  1782. */
  1783. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1784. int64_t reordered_pts, int64_t dts)
  1785. {
  1786. int64_t pts = AV_NOPTS_VALUE;
  1787. if (dts != AV_NOPTS_VALUE) {
  1788. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1789. ctx->pts_correction_last_dts = dts;
  1790. } else if (reordered_pts != AV_NOPTS_VALUE)
  1791. ctx->pts_correction_last_dts = reordered_pts;
  1792. if (reordered_pts != AV_NOPTS_VALUE) {
  1793. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1794. ctx->pts_correction_last_pts = reordered_pts;
  1795. } else if(dts != AV_NOPTS_VALUE)
  1796. ctx->pts_correction_last_pts = dts;
  1797. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1798. && reordered_pts != AV_NOPTS_VALUE)
  1799. pts = reordered_pts;
  1800. else
  1801. pts = dts;
  1802. return pts;
  1803. }
  1804. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1805. {
  1806. int size = 0, ret;
  1807. const uint8_t *data;
  1808. uint32_t flags;
  1809. int64_t val;
  1810. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1811. if (!data)
  1812. return 0;
  1813. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  1814. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1815. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1816. ret = AVERROR(EINVAL);
  1817. goto fail2;
  1818. }
  1819. if (size < 4)
  1820. goto fail;
  1821. flags = bytestream_get_le32(&data);
  1822. size -= 4;
  1823. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1824. if (size < 4)
  1825. goto fail;
  1826. val = bytestream_get_le32(&data);
  1827. if (val <= 0 || val > INT_MAX) {
  1828. av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
  1829. ret = AVERROR_INVALIDDATA;
  1830. goto fail2;
  1831. }
  1832. avctx->channels = val;
  1833. size -= 4;
  1834. }
  1835. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1836. if (size < 8)
  1837. goto fail;
  1838. avctx->channel_layout = bytestream_get_le64(&data);
  1839. size -= 8;
  1840. }
  1841. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1842. if (size < 4)
  1843. goto fail;
  1844. val = bytestream_get_le32(&data);
  1845. if (val <= 0 || val > INT_MAX) {
  1846. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
  1847. ret = AVERROR_INVALIDDATA;
  1848. goto fail2;
  1849. }
  1850. avctx->sample_rate = val;
  1851. size -= 4;
  1852. }
  1853. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1854. if (size < 8)
  1855. goto fail;
  1856. avctx->width = bytestream_get_le32(&data);
  1857. avctx->height = bytestream_get_le32(&data);
  1858. size -= 8;
  1859. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1860. if (ret < 0)
  1861. goto fail2;
  1862. }
  1863. return 0;
  1864. fail:
  1865. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1866. ret = AVERROR_INVALIDDATA;
  1867. fail2:
  1868. if (ret < 0) {
  1869. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1870. if (avctx->err_recognition & AV_EF_EXPLODE)
  1871. return ret;
  1872. }
  1873. return 0;
  1874. }
  1875. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1876. {
  1877. int ret;
  1878. /* move the original frame to our backup */
  1879. av_frame_unref(avci->to_free);
  1880. av_frame_move_ref(avci->to_free, frame);
  1881. /* now copy everything except the AVBufferRefs back
  1882. * note that we make a COPY of the side data, so calling av_frame_free() on
  1883. * the caller's frame will work properly */
  1884. ret = av_frame_copy_props(frame, avci->to_free);
  1885. if (ret < 0)
  1886. return ret;
  1887. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1888. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1889. if (avci->to_free->extended_data != avci->to_free->data) {
  1890. int planes = av_frame_get_channels(avci->to_free);
  1891. int size = planes * sizeof(*frame->extended_data);
  1892. if (!size) {
  1893. av_frame_unref(frame);
  1894. return AVERROR_BUG;
  1895. }
  1896. frame->extended_data = av_malloc(size);
  1897. if (!frame->extended_data) {
  1898. av_frame_unref(frame);
  1899. return AVERROR(ENOMEM);
  1900. }
  1901. memcpy(frame->extended_data, avci->to_free->extended_data,
  1902. size);
  1903. } else
  1904. frame->extended_data = frame->data;
  1905. frame->format = avci->to_free->format;
  1906. frame->width = avci->to_free->width;
  1907. frame->height = avci->to_free->height;
  1908. frame->channel_layout = avci->to_free->channel_layout;
  1909. frame->nb_samples = avci->to_free->nb_samples;
  1910. av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
  1911. return 0;
  1912. }
  1913. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1914. int *got_picture_ptr,
  1915. const AVPacket *avpkt)
  1916. {
  1917. AVCodecInternal *avci = avctx->internal;
  1918. int ret;
  1919. // copy to ensure we do not change avpkt
  1920. AVPacket tmp = *avpkt;
  1921. if (!avctx->codec)
  1922. return AVERROR(EINVAL);
  1923. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1924. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1925. return AVERROR(EINVAL);
  1926. }
  1927. if (!avctx->codec->decode) {
  1928. av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
  1929. return AVERROR(ENOSYS);
  1930. }
  1931. *got_picture_ptr = 0;
  1932. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1933. return AVERROR(EINVAL);
  1934. avctx->internal->pkt = avpkt;
  1935. ret = apply_param_change(avctx, avpkt);
  1936. if (ret < 0)
  1937. return ret;
  1938. av_frame_unref(picture);
  1939. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
  1940. (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1941. int did_split = av_packet_split_side_data(&tmp);
  1942. ret = apply_param_change(avctx, &tmp);
  1943. if (ret < 0)
  1944. goto fail;
  1945. avctx->internal->pkt = &tmp;
  1946. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1947. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1948. &tmp);
  1949. else {
  1950. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1951. &tmp);
  1952. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  1953. picture->pkt_dts = avpkt->dts;
  1954. if(!avctx->has_b_frames){
  1955. av_frame_set_pkt_pos(picture, avpkt->pos);
  1956. }
  1957. //FIXME these should be under if(!avctx->has_b_frames)
  1958. /* get_buffer is supposed to set frame parameters */
  1959. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  1960. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1961. if (!picture->width) picture->width = avctx->width;
  1962. if (!picture->height) picture->height = avctx->height;
  1963. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1964. }
  1965. }
  1966. fail:
  1967. emms_c(); //needed to avoid an emms_c() call before every return;
  1968. avctx->internal->pkt = NULL;
  1969. if (did_split) {
  1970. av_packet_free_side_data(&tmp);
  1971. if(ret == tmp.size)
  1972. ret = avpkt->size;
  1973. }
  1974. if (picture->flags & AV_FRAME_FLAG_DISCARD) {
  1975. *got_picture_ptr = 0;
  1976. }
  1977. if (*got_picture_ptr) {
  1978. if (!avctx->refcounted_frames) {
  1979. int err = unrefcount_frame(avci, picture);
  1980. if (err < 0)
  1981. return err;
  1982. }
  1983. avctx->frame_number++;
  1984. av_frame_set_best_effort_timestamp(picture,
  1985. guess_correct_pts(avctx,
  1986. picture->pkt_pts,
  1987. picture->pkt_dts));
  1988. } else
  1989. av_frame_unref(picture);
  1990. } else
  1991. ret = 0;
  1992. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1993. * make sure it's set correctly */
  1994. av_assert0(!picture->extended_data || picture->extended_data == picture->data);
  1995. #if FF_API_AVCTX_TIMEBASE
  1996. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1997. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  1998. #endif
  1999. return ret;
  2000. }
  2001. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  2002. AVFrame *frame,
  2003. int *got_frame_ptr,
  2004. const AVPacket *avpkt)
  2005. {
  2006. AVCodecInternal *avci = avctx->internal;
  2007. int ret = 0;
  2008. *got_frame_ptr = 0;
  2009. if (!avctx->codec)
  2010. return AVERROR(EINVAL);
  2011. if (!avctx->codec->decode) {
  2012. av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
  2013. return AVERROR(ENOSYS);
  2014. }
  2015. if (!avpkt->data && avpkt->size) {
  2016. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2017. return AVERROR(EINVAL);
  2018. }
  2019. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  2020. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  2021. return AVERROR(EINVAL);
  2022. }
  2023. av_frame_unref(frame);
  2024. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  2025. uint8_t *side;
  2026. int side_size;
  2027. uint32_t discard_padding = 0;
  2028. uint8_t skip_reason = 0;
  2029. uint8_t discard_reason = 0;
  2030. // copy to ensure we do not change avpkt
  2031. AVPacket tmp = *avpkt;
  2032. int did_split = av_packet_split_side_data(&tmp);
  2033. ret = apply_param_change(avctx, &tmp);
  2034. if (ret < 0)
  2035. goto fail;
  2036. avctx->internal->pkt = &tmp;
  2037. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2038. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  2039. else {
  2040. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  2041. av_assert0(ret <= tmp.size);
  2042. frame->pkt_dts = avpkt->dts;
  2043. }
  2044. if (ret >= 0 && *got_frame_ptr) {
  2045. avctx->frame_number++;
  2046. av_frame_set_best_effort_timestamp(frame,
  2047. guess_correct_pts(avctx,
  2048. frame->pkt_pts,
  2049. frame->pkt_dts));
  2050. if (frame->format == AV_SAMPLE_FMT_NONE)
  2051. frame->format = avctx->sample_fmt;
  2052. if (!frame->channel_layout)
  2053. frame->channel_layout = avctx->channel_layout;
  2054. if (!av_frame_get_channels(frame))
  2055. av_frame_set_channels(frame, avctx->channels);
  2056. if (!frame->sample_rate)
  2057. frame->sample_rate = avctx->sample_rate;
  2058. }
  2059. side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  2060. if(side && side_size>=10) {
  2061. avctx->internal->skip_samples = AV_RL32(side);
  2062. discard_padding = AV_RL32(side + 4);
  2063. av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
  2064. avctx->internal->skip_samples, (int)discard_padding);
  2065. skip_reason = AV_RL8(side + 8);
  2066. discard_reason = AV_RL8(side + 9);
  2067. }
  2068. if ((frame->flags & AV_FRAME_FLAG_DISCARD) && *got_frame_ptr &&
  2069. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  2070. avctx->internal->skip_samples -= frame->nb_samples;
  2071. *got_frame_ptr = 0;
  2072. }
  2073. if (avctx->internal->skip_samples > 0 && *got_frame_ptr &&
  2074. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  2075. if(frame->nb_samples <= avctx->internal->skip_samples){
  2076. *got_frame_ptr = 0;
  2077. avctx->internal->skip_samples -= frame->nb_samples;
  2078. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  2079. avctx->internal->skip_samples);
  2080. } else {
  2081. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  2082. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  2083. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2084. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  2085. (AVRational){1, avctx->sample_rate},
  2086. avctx->pkt_timebase);
  2087. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  2088. frame->pkt_pts += diff_ts;
  2089. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  2090. frame->pkt_dts += diff_ts;
  2091. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2092. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2093. } else {
  2094. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  2095. }
  2096. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  2097. avctx->internal->skip_samples, frame->nb_samples);
  2098. frame->nb_samples -= avctx->internal->skip_samples;
  2099. avctx->internal->skip_samples = 0;
  2100. }
  2101. }
  2102. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
  2103. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  2104. if (discard_padding == frame->nb_samples) {
  2105. *got_frame_ptr = 0;
  2106. } else {
  2107. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2108. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  2109. (AVRational){1, avctx->sample_rate},
  2110. avctx->pkt_timebase);
  2111. av_frame_set_pkt_duration(frame, diff_ts);
  2112. } else {
  2113. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  2114. }
  2115. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  2116. (int)discard_padding, frame->nb_samples);
  2117. frame->nb_samples -= discard_padding;
  2118. }
  2119. }
  2120. if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
  2121. AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
  2122. if (fside) {
  2123. AV_WL32(fside->data, avctx->internal->skip_samples);
  2124. AV_WL32(fside->data + 4, discard_padding);
  2125. AV_WL8(fside->data + 8, skip_reason);
  2126. AV_WL8(fside->data + 9, discard_reason);
  2127. avctx->internal->skip_samples = 0;
  2128. }
  2129. }
  2130. fail:
  2131. avctx->internal->pkt = NULL;
  2132. if (did_split) {
  2133. av_packet_free_side_data(&tmp);
  2134. if(ret == tmp.size)
  2135. ret = avpkt->size;
  2136. }
  2137. if (ret >= 0 && *got_frame_ptr) {
  2138. if (!avctx->refcounted_frames) {
  2139. int err = unrefcount_frame(avci, frame);
  2140. if (err < 0)
  2141. return err;
  2142. }
  2143. } else
  2144. av_frame_unref(frame);
  2145. }
  2146. av_assert0(ret <= avpkt->size);
  2147. if (!avci->showed_multi_packet_warning &&
  2148. ret >= 0 && ret != avpkt->size && !(avctx->codec->capabilities & AV_CODEC_CAP_SUBFRAMES)) {
  2149. av_log(avctx, AV_LOG_WARNING, "Multiple frames in a packet.\n");
  2150. avci->showed_multi_packet_warning = 1;
  2151. }
  2152. return ret;
  2153. }
  2154. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2155. static int recode_subtitle(AVCodecContext *avctx,
  2156. AVPacket *outpkt, const AVPacket *inpkt)
  2157. {
  2158. #if CONFIG_ICONV
  2159. iconv_t cd = (iconv_t)-1;
  2160. int ret = 0;
  2161. char *inb, *outb;
  2162. size_t inl, outl;
  2163. AVPacket tmp;
  2164. #endif
  2165. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2166. return 0;
  2167. #if CONFIG_ICONV
  2168. cd = iconv_open("UTF-8", avctx->sub_charenc);
  2169. av_assert0(cd != (iconv_t)-1);
  2170. inb = inpkt->data;
  2171. inl = inpkt->size;
  2172. if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
  2173. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2174. ret = AVERROR(ENOMEM);
  2175. goto end;
  2176. }
  2177. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2178. if (ret < 0)
  2179. goto end;
  2180. outpkt->buf = tmp.buf;
  2181. outpkt->data = tmp.data;
  2182. outpkt->size = tmp.size;
  2183. outb = outpkt->data;
  2184. outl = outpkt->size;
  2185. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2186. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2187. outl >= outpkt->size || inl != 0) {
  2188. ret = FFMIN(AVERROR(errno), -1);
  2189. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2190. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2191. av_packet_unref(&tmp);
  2192. goto end;
  2193. }
  2194. outpkt->size -= outl;
  2195. memset(outpkt->data + outpkt->size, 0, outl);
  2196. end:
  2197. if (cd != (iconv_t)-1)
  2198. iconv_close(cd);
  2199. return ret;
  2200. #else
  2201. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  2202. return AVERROR(EINVAL);
  2203. #endif
  2204. }
  2205. static int utf8_check(const uint8_t *str)
  2206. {
  2207. const uint8_t *byte;
  2208. uint32_t codepoint, min;
  2209. while (*str) {
  2210. byte = str;
  2211. GET_UTF8(codepoint, *(byte++), return 0;);
  2212. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2213. 1 << (5 * (byte - str) - 4);
  2214. if (codepoint < min || codepoint >= 0x110000 ||
  2215. codepoint == 0xFFFE /* BOM */ ||
  2216. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2217. return 0;
  2218. str = byte;
  2219. }
  2220. return 1;
  2221. }
  2222. #if FF_API_ASS_TIMING
  2223. static void insert_ts(AVBPrint *buf, int ts)
  2224. {
  2225. if (ts == -1) {
  2226. av_bprintf(buf, "9:59:59.99,");
  2227. } else {
  2228. int h, m, s;
  2229. h = ts/360000; ts -= 360000*h;
  2230. m = ts/ 6000; ts -= 6000*m;
  2231. s = ts/ 100; ts -= 100*s;
  2232. av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
  2233. }
  2234. }
  2235. static int convert_sub_to_old_ass_form(AVSubtitle *sub, const AVPacket *pkt, AVRational tb)
  2236. {
  2237. int i;
  2238. AVBPrint buf;
  2239. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  2240. for (i = 0; i < sub->num_rects; i++) {
  2241. char *final_dialog;
  2242. const char *dialog;
  2243. AVSubtitleRect *rect = sub->rects[i];
  2244. int ts_start, ts_duration = -1;
  2245. long int layer;
  2246. if (rect->type != SUBTITLE_ASS || !strncmp(rect->ass, "Dialogue: ", 10))
  2247. continue;
  2248. av_bprint_clear(&buf);
  2249. /* skip ReadOrder */
  2250. dialog = strchr(rect->ass, ',');
  2251. if (!dialog)
  2252. continue;
  2253. dialog++;
  2254. /* extract Layer or Marked */
  2255. layer = strtol(dialog, (char**)&dialog, 10);
  2256. if (*dialog != ',')
  2257. continue;
  2258. dialog++;
  2259. /* rescale timing to ASS time base (ms) */
  2260. ts_start = av_rescale_q(pkt->pts, tb, av_make_q(1, 100));
  2261. if (pkt->duration != -1)
  2262. ts_duration = av_rescale_q(pkt->duration, tb, av_make_q(1, 100));
  2263. sub->end_display_time = FFMAX(sub->end_display_time, 10 * ts_duration);
  2264. /* construct ASS (standalone file form with timestamps) string */
  2265. av_bprintf(&buf, "Dialogue: %ld,", layer);
  2266. insert_ts(&buf, ts_start);
  2267. insert_ts(&buf, ts_duration == -1 ? -1 : ts_start + ts_duration);
  2268. av_bprintf(&buf, "%s\r\n", dialog);
  2269. final_dialog = av_strdup(buf.str);
  2270. if (!av_bprint_is_complete(&buf) || !final_dialog) {
  2271. av_freep(&final_dialog);
  2272. av_bprint_finalize(&buf, NULL);
  2273. return AVERROR(ENOMEM);
  2274. }
  2275. av_freep(&rect->ass);
  2276. rect->ass = final_dialog;
  2277. }
  2278. av_bprint_finalize(&buf, NULL);
  2279. return 0;
  2280. }
  2281. #endif
  2282. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2283. int *got_sub_ptr,
  2284. AVPacket *avpkt)
  2285. {
  2286. int i, ret = 0;
  2287. if (!avpkt->data && avpkt->size) {
  2288. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2289. return AVERROR(EINVAL);
  2290. }
  2291. if (!avctx->codec)
  2292. return AVERROR(EINVAL);
  2293. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2294. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2295. return AVERROR(EINVAL);
  2296. }
  2297. *got_sub_ptr = 0;
  2298. get_subtitle_defaults(sub);
  2299. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  2300. AVPacket pkt_recoded;
  2301. AVPacket tmp = *avpkt;
  2302. int did_split = av_packet_split_side_data(&tmp);
  2303. //apply_param_change(avctx, &tmp);
  2304. if (did_split) {
  2305. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  2306. * proper padding.
  2307. * If the side data is smaller than the buffer padding size, the
  2308. * remaining bytes should have already been filled with zeros by the
  2309. * original packet allocation anyway. */
  2310. memset(tmp.data + tmp.size, 0,
  2311. FFMIN(avpkt->size - tmp.size, AV_INPUT_BUFFER_PADDING_SIZE));
  2312. }
  2313. pkt_recoded = tmp;
  2314. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2315. if (ret < 0) {
  2316. *got_sub_ptr = 0;
  2317. } else {
  2318. avctx->internal->pkt = &pkt_recoded;
  2319. if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
  2320. sub->pts = av_rescale_q(avpkt->pts,
  2321. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2322. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2323. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2324. !!*got_sub_ptr >= !!sub->num_rects);
  2325. #if FF_API_ASS_TIMING
  2326. if (avctx->sub_text_format == FF_SUB_TEXT_FMT_ASS_WITH_TIMINGS
  2327. && *got_sub_ptr && sub->num_rects) {
  2328. const AVRational tb = avctx->pkt_timebase.num ? avctx->pkt_timebase
  2329. : avctx->time_base;
  2330. int err = convert_sub_to_old_ass_form(sub, avpkt, tb);
  2331. if (err < 0)
  2332. ret = err;
  2333. }
  2334. #endif
  2335. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2336. avctx->pkt_timebase.num) {
  2337. AVRational ms = { 1, 1000 };
  2338. sub->end_display_time = av_rescale_q(avpkt->duration,
  2339. avctx->pkt_timebase, ms);
  2340. }
  2341. for (i = 0; i < sub->num_rects; i++) {
  2342. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2343. av_log(avctx, AV_LOG_ERROR,
  2344. "Invalid UTF-8 in decoded subtitles text; "
  2345. "maybe missing -sub_charenc option\n");
  2346. avsubtitle_free(sub);
  2347. return AVERROR_INVALIDDATA;
  2348. }
  2349. }
  2350. if (tmp.data != pkt_recoded.data) { // did we recode?
  2351. /* prevent from destroying side data from original packet */
  2352. pkt_recoded.side_data = NULL;
  2353. pkt_recoded.side_data_elems = 0;
  2354. av_packet_unref(&pkt_recoded);
  2355. }
  2356. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2357. sub->format = 0;
  2358. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2359. sub->format = 1;
  2360. avctx->internal->pkt = NULL;
  2361. }
  2362. if (did_split) {
  2363. av_packet_free_side_data(&tmp);
  2364. if(ret == tmp.size)
  2365. ret = avpkt->size;
  2366. }
  2367. if (*got_sub_ptr)
  2368. avctx->frame_number++;
  2369. }
  2370. return ret;
  2371. }
  2372. void avsubtitle_free(AVSubtitle *sub)
  2373. {
  2374. int i;
  2375. for (i = 0; i < sub->num_rects; i++) {
  2376. av_freep(&sub->rects[i]->data[0]);
  2377. av_freep(&sub->rects[i]->data[1]);
  2378. av_freep(&sub->rects[i]->data[2]);
  2379. av_freep(&sub->rects[i]->data[3]);
  2380. av_freep(&sub->rects[i]->text);
  2381. av_freep(&sub->rects[i]->ass);
  2382. av_freep(&sub->rects[i]);
  2383. }
  2384. av_freep(&sub->rects);
  2385. memset(sub, 0, sizeof(AVSubtitle));
  2386. }
  2387. static int do_decode(AVCodecContext *avctx, AVPacket *pkt)
  2388. {
  2389. int got_frame;
  2390. int ret;
  2391. av_assert0(!avctx->internal->buffer_frame->buf[0]);
  2392. if (!pkt)
  2393. pkt = avctx->internal->buffer_pkt;
  2394. // This is the lesser evil. The field is for compatibility with legacy users
  2395. // of the legacy API, and users using the new API should not be forced to
  2396. // even know about this field.
  2397. avctx->refcounted_frames = 1;
  2398. // Some codecs (at least wma lossless) will crash when feeding drain packets
  2399. // after EOF was signaled.
  2400. if (avctx->internal->draining_done)
  2401. return AVERROR_EOF;
  2402. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  2403. ret = avcodec_decode_video2(avctx, avctx->internal->buffer_frame,
  2404. &got_frame, pkt);
  2405. if (ret >= 0)
  2406. ret = pkt->size;
  2407. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  2408. ret = avcodec_decode_audio4(avctx, avctx->internal->buffer_frame,
  2409. &got_frame, pkt);
  2410. } else {
  2411. ret = AVERROR(EINVAL);
  2412. }
  2413. if (ret == AVERROR(EAGAIN))
  2414. ret = pkt->size;
  2415. if (ret < 0)
  2416. return ret;
  2417. if (avctx->internal->draining && !got_frame)
  2418. avctx->internal->draining_done = 1;
  2419. if (ret >= pkt->size) {
  2420. av_packet_unref(avctx->internal->buffer_pkt);
  2421. } else {
  2422. int consumed = ret;
  2423. if (pkt != avctx->internal->buffer_pkt) {
  2424. av_packet_unref(avctx->internal->buffer_pkt);
  2425. if ((ret = av_packet_ref(avctx->internal->buffer_pkt, pkt)) < 0)
  2426. return ret;
  2427. }
  2428. avctx->internal->buffer_pkt->data += consumed;
  2429. avctx->internal->buffer_pkt->size -= consumed;
  2430. avctx->internal->buffer_pkt->pts = AV_NOPTS_VALUE;
  2431. avctx->internal->buffer_pkt->dts = AV_NOPTS_VALUE;
  2432. }
  2433. if (got_frame)
  2434. av_assert0(avctx->internal->buffer_frame->buf[0]);
  2435. return 0;
  2436. }
  2437. int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  2438. {
  2439. int ret;
  2440. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  2441. return AVERROR(EINVAL);
  2442. if (avctx->internal->draining)
  2443. return AVERROR_EOF;
  2444. if (avpkt && !avpkt->size && avpkt->data)
  2445. return AVERROR(EINVAL);
  2446. if (!avpkt || !avpkt->size) {
  2447. avctx->internal->draining = 1;
  2448. avpkt = NULL;
  2449. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  2450. return 0;
  2451. }
  2452. if (avctx->codec->send_packet) {
  2453. if (avpkt) {
  2454. AVPacket tmp = *avpkt;
  2455. int did_split = av_packet_split_side_data(&tmp);
  2456. ret = apply_param_change(avctx, &tmp);
  2457. if (ret >= 0)
  2458. ret = avctx->codec->send_packet(avctx, &tmp);
  2459. if (did_split)
  2460. av_packet_free_side_data(&tmp);
  2461. return ret;
  2462. } else {
  2463. return avctx->codec->send_packet(avctx, NULL);
  2464. }
  2465. }
  2466. // Emulation via old API. Assume avpkt is likely not refcounted, while
  2467. // decoder output is always refcounted, and avoid copying.
  2468. if (avctx->internal->buffer_pkt->size || avctx->internal->buffer_frame->buf[0])
  2469. return AVERROR(EAGAIN);
  2470. // The goal is decoding the first frame of the packet without using memcpy,
  2471. // because the common case is having only 1 frame per packet (especially
  2472. // with video, but audio too). In other cases, it can't be avoided, unless
  2473. // the user is feeding refcounted packets.
  2474. return do_decode(avctx, (AVPacket *)avpkt);
  2475. }
  2476. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  2477. {
  2478. int ret;
  2479. av_frame_unref(frame);
  2480. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  2481. return AVERROR(EINVAL);
  2482. if (avctx->codec->receive_frame) {
  2483. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  2484. return AVERROR_EOF;
  2485. ret = avctx->codec->receive_frame(avctx, frame);
  2486. if (ret >= 0) {
  2487. if (av_frame_get_best_effort_timestamp(frame) == AV_NOPTS_VALUE) {
  2488. av_frame_set_best_effort_timestamp(frame,
  2489. guess_correct_pts(avctx, frame->pkt_pts, frame->pkt_dts));
  2490. }
  2491. }
  2492. return ret;
  2493. }
  2494. // Emulation via old API.
  2495. if (!avctx->internal->buffer_frame->buf[0]) {
  2496. if (!avctx->internal->buffer_pkt->size && !avctx->internal->draining)
  2497. return AVERROR(EAGAIN);
  2498. while (1) {
  2499. if ((ret = do_decode(avctx, avctx->internal->buffer_pkt)) < 0) {
  2500. av_packet_unref(avctx->internal->buffer_pkt);
  2501. return ret;
  2502. }
  2503. // Some audio decoders may consume partial data without returning
  2504. // a frame (fate-wmapro-2ch). There is no way to make the caller
  2505. // call avcodec_receive_frame() again without returning a frame,
  2506. // so try to decode more in these cases.
  2507. if (avctx->internal->buffer_frame->buf[0] ||
  2508. !avctx->internal->buffer_pkt->size)
  2509. break;
  2510. }
  2511. }
  2512. if (!avctx->internal->buffer_frame->buf[0])
  2513. return avctx->internal->draining ? AVERROR_EOF : AVERROR(EAGAIN);
  2514. av_frame_move_ref(frame, avctx->internal->buffer_frame);
  2515. return 0;
  2516. }
  2517. static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
  2518. {
  2519. int ret;
  2520. *got_packet = 0;
  2521. av_packet_unref(avctx->internal->buffer_pkt);
  2522. avctx->internal->buffer_pkt_valid = 0;
  2523. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  2524. ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
  2525. frame, got_packet);
  2526. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  2527. ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
  2528. frame, got_packet);
  2529. } else {
  2530. ret = AVERROR(EINVAL);
  2531. }
  2532. if (ret >= 0 && *got_packet) {
  2533. // Encoders must always return ref-counted buffers.
  2534. // Side-data only packets have no data and can be not ref-counted.
  2535. av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
  2536. avctx->internal->buffer_pkt_valid = 1;
  2537. ret = 0;
  2538. } else {
  2539. av_packet_unref(avctx->internal->buffer_pkt);
  2540. }
  2541. return ret;
  2542. }
  2543. int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  2544. {
  2545. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  2546. return AVERROR(EINVAL);
  2547. if (avctx->internal->draining)
  2548. return AVERROR_EOF;
  2549. if (!frame) {
  2550. avctx->internal->draining = 1;
  2551. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  2552. return 0;
  2553. }
  2554. if (avctx->codec->send_frame)
  2555. return avctx->codec->send_frame(avctx, frame);
  2556. // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
  2557. // 1. if the AVFrame is not refcounted, the copying will be much more
  2558. // expensive than copying the packet data
  2559. // 2. assume few users use non-refcounted AVPackets, so usually no copy is
  2560. // needed
  2561. if (avctx->internal->buffer_pkt_valid)
  2562. return AVERROR(EAGAIN);
  2563. return do_encode(avctx, frame, &(int){0});
  2564. }
  2565. int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  2566. {
  2567. av_packet_unref(avpkt);
  2568. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  2569. return AVERROR(EINVAL);
  2570. if (avctx->codec->receive_packet) {
  2571. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  2572. return AVERROR_EOF;
  2573. return avctx->codec->receive_packet(avctx, avpkt);
  2574. }
  2575. // Emulation via old API.
  2576. if (!avctx->internal->buffer_pkt_valid) {
  2577. int got_packet;
  2578. int ret;
  2579. if (!avctx->internal->draining)
  2580. return AVERROR(EAGAIN);
  2581. ret = do_encode(avctx, NULL, &got_packet);
  2582. if (ret < 0)
  2583. return ret;
  2584. if (ret >= 0 && !got_packet)
  2585. return AVERROR_EOF;
  2586. }
  2587. av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
  2588. avctx->internal->buffer_pkt_valid = 0;
  2589. return 0;
  2590. }
  2591. av_cold int avcodec_close(AVCodecContext *avctx)
  2592. {
  2593. int i;
  2594. if (!avctx)
  2595. return 0;
  2596. if (avcodec_is_open(avctx)) {
  2597. FramePool *pool = avctx->internal->pool;
  2598. if (CONFIG_FRAME_THREAD_ENCODER &&
  2599. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2600. ff_frame_thread_encoder_free(avctx);
  2601. }
  2602. if (HAVE_THREADS && avctx->internal->thread_ctx)
  2603. ff_thread_free(avctx);
  2604. if (avctx->codec && avctx->codec->close)
  2605. avctx->codec->close(avctx);
  2606. avctx->internal->byte_buffer_size = 0;
  2607. av_freep(&avctx->internal->byte_buffer);
  2608. av_frame_free(&avctx->internal->to_free);
  2609. av_frame_free(&avctx->internal->buffer_frame);
  2610. av_packet_free(&avctx->internal->buffer_pkt);
  2611. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2612. av_buffer_pool_uninit(&pool->pools[i]);
  2613. av_freep(&avctx->internal->pool);
  2614. if (avctx->hwaccel && avctx->hwaccel->uninit)
  2615. avctx->hwaccel->uninit(avctx);
  2616. av_freep(&avctx->internal->hwaccel_priv_data);
  2617. av_freep(&avctx->internal);
  2618. }
  2619. for (i = 0; i < avctx->nb_coded_side_data; i++)
  2620. av_freep(&avctx->coded_side_data[i].data);
  2621. av_freep(&avctx->coded_side_data);
  2622. avctx->nb_coded_side_data = 0;
  2623. av_buffer_unref(&avctx->hw_frames_ctx);
  2624. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2625. av_opt_free(avctx->priv_data);
  2626. av_opt_free(avctx);
  2627. av_freep(&avctx->priv_data);
  2628. if (av_codec_is_encoder(avctx->codec)) {
  2629. av_freep(&avctx->extradata);
  2630. #if FF_API_CODED_FRAME
  2631. FF_DISABLE_DEPRECATION_WARNINGS
  2632. av_frame_free(&avctx->coded_frame);
  2633. FF_ENABLE_DEPRECATION_WARNINGS
  2634. #endif
  2635. }
  2636. avctx->codec = NULL;
  2637. avctx->active_thread_type = 0;
  2638. return 0;
  2639. }
  2640. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2641. {
  2642. switch(id){
  2643. //This is for future deprecatec codec ids, its empty since
  2644. //last major bump but will fill up again over time, please don't remove it
  2645. default : return id;
  2646. }
  2647. }
  2648. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2649. {
  2650. AVCodec *p, *experimental = NULL;
  2651. p = first_avcodec;
  2652. id= remap_deprecated_codec_id(id);
  2653. while (p) {
  2654. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2655. p->id == id) {
  2656. if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
  2657. experimental = p;
  2658. } else
  2659. return p;
  2660. }
  2661. p = p->next;
  2662. }
  2663. return experimental;
  2664. }
  2665. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2666. {
  2667. return find_encdec(id, 1);
  2668. }
  2669. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2670. {
  2671. AVCodec *p;
  2672. if (!name)
  2673. return NULL;
  2674. p = first_avcodec;
  2675. while (p) {
  2676. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2677. return p;
  2678. p = p->next;
  2679. }
  2680. return NULL;
  2681. }
  2682. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2683. {
  2684. return find_encdec(id, 0);
  2685. }
  2686. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2687. {
  2688. AVCodec *p;
  2689. if (!name)
  2690. return NULL;
  2691. p = first_avcodec;
  2692. while (p) {
  2693. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2694. return p;
  2695. p = p->next;
  2696. }
  2697. return NULL;
  2698. }
  2699. const char *avcodec_get_name(enum AVCodecID id)
  2700. {
  2701. const AVCodecDescriptor *cd;
  2702. AVCodec *codec;
  2703. if (id == AV_CODEC_ID_NONE)
  2704. return "none";
  2705. cd = avcodec_descriptor_get(id);
  2706. if (cd)
  2707. return cd->name;
  2708. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2709. codec = avcodec_find_decoder(id);
  2710. if (codec)
  2711. return codec->name;
  2712. codec = avcodec_find_encoder(id);
  2713. if (codec)
  2714. return codec->name;
  2715. return "unknown_codec";
  2716. }
  2717. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2718. {
  2719. int i, len, ret = 0;
  2720. #define TAG_PRINT(x) \
  2721. (((x) >= '0' && (x) <= '9') || \
  2722. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2723. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2724. for (i = 0; i < 4; i++) {
  2725. len = snprintf(buf, buf_size,
  2726. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2727. buf += len;
  2728. buf_size = buf_size > len ? buf_size - len : 0;
  2729. ret += len;
  2730. codec_tag >>= 8;
  2731. }
  2732. return ret;
  2733. }
  2734. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2735. {
  2736. const char *codec_type;
  2737. const char *codec_name;
  2738. const char *profile = NULL;
  2739. int64_t bitrate;
  2740. int new_line = 0;
  2741. AVRational display_aspect_ratio;
  2742. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  2743. if (!buf || buf_size <= 0)
  2744. return;
  2745. codec_type = av_get_media_type_string(enc->codec_type);
  2746. codec_name = avcodec_get_name(enc->codec_id);
  2747. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  2748. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2749. codec_name);
  2750. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2751. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2752. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2753. if (profile)
  2754. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2755. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  2756. && av_log_get_level() >= AV_LOG_VERBOSE
  2757. && enc->refs)
  2758. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2759. ", %d reference frame%s",
  2760. enc->refs, enc->refs > 1 ? "s" : "");
  2761. if (enc->codec_tag) {
  2762. char tag_buf[32];
  2763. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2764. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2765. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2766. }
  2767. switch (enc->codec_type) {
  2768. case AVMEDIA_TYPE_VIDEO:
  2769. {
  2770. char detail[256] = "(";
  2771. av_strlcat(buf, separator, buf_size);
  2772. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2773. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  2774. av_get_pix_fmt_name(enc->pix_fmt));
  2775. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  2776. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  2777. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2778. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2779. av_strlcatf(detail, sizeof(detail), "%s, ",
  2780. av_color_range_name(enc->color_range));
  2781. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  2782. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  2783. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  2784. if (enc->colorspace != (int)enc->color_primaries ||
  2785. enc->colorspace != (int)enc->color_trc) {
  2786. new_line = 1;
  2787. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  2788. av_color_space_name(enc->colorspace),
  2789. av_color_primaries_name(enc->color_primaries),
  2790. av_color_transfer_name(enc->color_trc));
  2791. } else
  2792. av_strlcatf(detail, sizeof(detail), "%s, ",
  2793. av_get_colorspace_name(enc->colorspace));
  2794. }
  2795. if (av_log_get_level() >= AV_LOG_DEBUG &&
  2796. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  2797. av_strlcatf(detail, sizeof(detail), "%s, ",
  2798. av_chroma_location_name(enc->chroma_sample_location));
  2799. if (strlen(detail) > 1) {
  2800. detail[strlen(detail) - 2] = 0;
  2801. av_strlcatf(buf, buf_size, "%s)", detail);
  2802. }
  2803. }
  2804. if (enc->width) {
  2805. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  2806. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2807. "%dx%d",
  2808. enc->width, enc->height);
  2809. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  2810. (enc->width != enc->coded_width ||
  2811. enc->height != enc->coded_height))
  2812. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2813. " (%dx%d)", enc->coded_width, enc->coded_height);
  2814. if (enc->sample_aspect_ratio.num) {
  2815. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2816. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  2817. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  2818. 1024 * 1024);
  2819. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2820. " [SAR %d:%d DAR %d:%d]",
  2821. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2822. display_aspect_ratio.num, display_aspect_ratio.den);
  2823. }
  2824. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2825. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2826. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2827. ", %d/%d",
  2828. enc->time_base.num / g, enc->time_base.den / g);
  2829. }
  2830. }
  2831. if (encode) {
  2832. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2833. ", q=%d-%d", enc->qmin, enc->qmax);
  2834. } else {
  2835. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  2836. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2837. ", Closed Captions");
  2838. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  2839. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2840. ", lossless");
  2841. }
  2842. break;
  2843. case AVMEDIA_TYPE_AUDIO:
  2844. av_strlcat(buf, separator, buf_size);
  2845. if (enc->sample_rate) {
  2846. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2847. "%d Hz, ", enc->sample_rate);
  2848. }
  2849. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2850. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2851. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2852. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2853. }
  2854. if ( enc->bits_per_raw_sample > 0
  2855. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  2856. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2857. " (%d bit)", enc->bits_per_raw_sample);
  2858. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  2859. if (enc->initial_padding)
  2860. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2861. ", delay %d", enc->initial_padding);
  2862. if (enc->trailing_padding)
  2863. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2864. ", padding %d", enc->trailing_padding);
  2865. }
  2866. break;
  2867. case AVMEDIA_TYPE_DATA:
  2868. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2869. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2870. if (g)
  2871. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2872. ", %d/%d",
  2873. enc->time_base.num / g, enc->time_base.den / g);
  2874. }
  2875. break;
  2876. case AVMEDIA_TYPE_SUBTITLE:
  2877. if (enc->width)
  2878. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2879. ", %dx%d", enc->width, enc->height);
  2880. break;
  2881. default:
  2882. return;
  2883. }
  2884. if (encode) {
  2885. if (enc->flags & AV_CODEC_FLAG_PASS1)
  2886. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2887. ", pass 1");
  2888. if (enc->flags & AV_CODEC_FLAG_PASS2)
  2889. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2890. ", pass 2");
  2891. }
  2892. bitrate = get_bit_rate(enc);
  2893. if (bitrate != 0) {
  2894. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2895. ", %"PRId64" kb/s", bitrate / 1000);
  2896. } else if (enc->rc_max_rate > 0) {
  2897. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2898. ", max. %"PRId64" kb/s", (int64_t)enc->rc_max_rate / 1000);
  2899. }
  2900. }
  2901. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2902. {
  2903. const AVProfile *p;
  2904. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2905. return NULL;
  2906. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2907. if (p->profile == profile)
  2908. return p->name;
  2909. return NULL;
  2910. }
  2911. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  2912. {
  2913. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  2914. const AVProfile *p;
  2915. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  2916. return NULL;
  2917. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2918. if (p->profile == profile)
  2919. return p->name;
  2920. return NULL;
  2921. }
  2922. unsigned avcodec_version(void)
  2923. {
  2924. // av_assert0(AV_CODEC_ID_V410==164);
  2925. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2926. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2927. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2928. av_assert0(AV_CODEC_ID_SRT==94216);
  2929. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2930. return LIBAVCODEC_VERSION_INT;
  2931. }
  2932. const char *avcodec_configuration(void)
  2933. {
  2934. return FFMPEG_CONFIGURATION;
  2935. }
  2936. const char *avcodec_license(void)
  2937. {
  2938. #define LICENSE_PREFIX "libavcodec license: "
  2939. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2940. }
  2941. void avcodec_flush_buffers(AVCodecContext *avctx)
  2942. {
  2943. avctx->internal->draining = 0;
  2944. avctx->internal->draining_done = 0;
  2945. av_frame_unref(avctx->internal->buffer_frame);
  2946. av_packet_unref(avctx->internal->buffer_pkt);
  2947. avctx->internal->buffer_pkt_valid = 0;
  2948. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2949. ff_thread_flush(avctx);
  2950. else if (avctx->codec->flush)
  2951. avctx->codec->flush(avctx);
  2952. avctx->pts_correction_last_pts =
  2953. avctx->pts_correction_last_dts = INT64_MIN;
  2954. if (!avctx->refcounted_frames)
  2955. av_frame_unref(avctx->internal->to_free);
  2956. }
  2957. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2958. {
  2959. switch (codec_id) {
  2960. case AV_CODEC_ID_8SVX_EXP:
  2961. case AV_CODEC_ID_8SVX_FIB:
  2962. case AV_CODEC_ID_ADPCM_CT:
  2963. case AV_CODEC_ID_ADPCM_IMA_APC:
  2964. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2965. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2966. case AV_CODEC_ID_ADPCM_IMA_WS:
  2967. case AV_CODEC_ID_ADPCM_G722:
  2968. case AV_CODEC_ID_ADPCM_YAMAHA:
  2969. case AV_CODEC_ID_ADPCM_AICA:
  2970. return 4;
  2971. case AV_CODEC_ID_DSD_LSBF:
  2972. case AV_CODEC_ID_DSD_MSBF:
  2973. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  2974. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  2975. case AV_CODEC_ID_PCM_ALAW:
  2976. case AV_CODEC_ID_PCM_MULAW:
  2977. case AV_CODEC_ID_PCM_S8:
  2978. case AV_CODEC_ID_PCM_S8_PLANAR:
  2979. case AV_CODEC_ID_PCM_U8:
  2980. case AV_CODEC_ID_PCM_ZORK:
  2981. case AV_CODEC_ID_SDX2_DPCM:
  2982. return 8;
  2983. case AV_CODEC_ID_PCM_S16BE:
  2984. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2985. case AV_CODEC_ID_PCM_S16LE:
  2986. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2987. case AV_CODEC_ID_PCM_U16BE:
  2988. case AV_CODEC_ID_PCM_U16LE:
  2989. return 16;
  2990. case AV_CODEC_ID_PCM_S24DAUD:
  2991. case AV_CODEC_ID_PCM_S24BE:
  2992. case AV_CODEC_ID_PCM_S24LE:
  2993. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2994. case AV_CODEC_ID_PCM_U24BE:
  2995. case AV_CODEC_ID_PCM_U24LE:
  2996. return 24;
  2997. case AV_CODEC_ID_PCM_S32BE:
  2998. case AV_CODEC_ID_PCM_S32LE:
  2999. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  3000. case AV_CODEC_ID_PCM_U32BE:
  3001. case AV_CODEC_ID_PCM_U32LE:
  3002. case AV_CODEC_ID_PCM_F32BE:
  3003. case AV_CODEC_ID_PCM_F32LE:
  3004. return 32;
  3005. case AV_CODEC_ID_PCM_F64BE:
  3006. case AV_CODEC_ID_PCM_F64LE:
  3007. case AV_CODEC_ID_PCM_S64BE:
  3008. case AV_CODEC_ID_PCM_S64LE:
  3009. return 64;
  3010. default:
  3011. return 0;
  3012. }
  3013. }
  3014. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  3015. {
  3016. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  3017. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  3018. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  3019. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  3020. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  3021. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  3022. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  3023. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  3024. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  3025. [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
  3026. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  3027. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  3028. };
  3029. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  3030. return AV_CODEC_ID_NONE;
  3031. if (be < 0 || be > 1)
  3032. be = AV_NE(1, 0);
  3033. return map[fmt][be];
  3034. }
  3035. int av_get_bits_per_sample(enum AVCodecID codec_id)
  3036. {
  3037. switch (codec_id) {
  3038. case AV_CODEC_ID_ADPCM_SBPRO_2:
  3039. return 2;
  3040. case AV_CODEC_ID_ADPCM_SBPRO_3:
  3041. return 3;
  3042. case AV_CODEC_ID_ADPCM_SBPRO_4:
  3043. case AV_CODEC_ID_ADPCM_IMA_WAV:
  3044. case AV_CODEC_ID_ADPCM_IMA_QT:
  3045. case AV_CODEC_ID_ADPCM_SWF:
  3046. case AV_CODEC_ID_ADPCM_MS:
  3047. return 4;
  3048. default:
  3049. return av_get_exact_bits_per_sample(codec_id);
  3050. }
  3051. }
  3052. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  3053. uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
  3054. uint8_t * extradata, int frame_size, int frame_bytes)
  3055. {
  3056. int bps = av_get_exact_bits_per_sample(id);
  3057. int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
  3058. /* codecs with an exact constant bits per sample */
  3059. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  3060. return (frame_bytes * 8LL) / (bps * ch);
  3061. bps = bits_per_coded_sample;
  3062. /* codecs with a fixed packet duration */
  3063. switch (id) {
  3064. case AV_CODEC_ID_ADPCM_ADX: return 32;
  3065. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  3066. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  3067. case AV_CODEC_ID_AMR_NB:
  3068. case AV_CODEC_ID_EVRC:
  3069. case AV_CODEC_ID_GSM:
  3070. case AV_CODEC_ID_QCELP:
  3071. case AV_CODEC_ID_RA_288: return 160;
  3072. case AV_CODEC_ID_AMR_WB:
  3073. case AV_CODEC_ID_GSM_MS: return 320;
  3074. case AV_CODEC_ID_MP1: return 384;
  3075. case AV_CODEC_ID_ATRAC1: return 512;
  3076. case AV_CODEC_ID_ATRAC3: return 1024 * framecount;
  3077. case AV_CODEC_ID_ATRAC3P: return 2048;
  3078. case AV_CODEC_ID_MP2:
  3079. case AV_CODEC_ID_MUSEPACK7: return 1152;
  3080. case AV_CODEC_ID_AC3: return 1536;
  3081. }
  3082. if (sr > 0) {
  3083. /* calc from sample rate */
  3084. if (id == AV_CODEC_ID_TTA)
  3085. return 256 * sr / 245;
  3086. else if (id == AV_CODEC_ID_DST)
  3087. return 588 * sr / 44100;
  3088. if (ch > 0) {
  3089. /* calc from sample rate and channels */
  3090. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  3091. return (480 << (sr / 22050)) / ch;
  3092. }
  3093. }
  3094. if (ba > 0) {
  3095. /* calc from block_align */
  3096. if (id == AV_CODEC_ID_SIPR) {
  3097. switch (ba) {
  3098. case 20: return 160;
  3099. case 19: return 144;
  3100. case 29: return 288;
  3101. case 37: return 480;
  3102. }
  3103. } else if (id == AV_CODEC_ID_ILBC) {
  3104. switch (ba) {
  3105. case 38: return 160;
  3106. case 50: return 240;
  3107. }
  3108. }
  3109. }
  3110. if (frame_bytes > 0) {
  3111. /* calc from frame_bytes only */
  3112. if (id == AV_CODEC_ID_TRUESPEECH)
  3113. return 240 * (frame_bytes / 32);
  3114. if (id == AV_CODEC_ID_NELLYMOSER)
  3115. return 256 * (frame_bytes / 64);
  3116. if (id == AV_CODEC_ID_RA_144)
  3117. return 160 * (frame_bytes / 20);
  3118. if (id == AV_CODEC_ID_G723_1)
  3119. return 240 * (frame_bytes / 24);
  3120. if (bps > 0) {
  3121. /* calc from frame_bytes and bits_per_coded_sample */
  3122. if (id == AV_CODEC_ID_ADPCM_G726)
  3123. return frame_bytes * 8 / bps;
  3124. }
  3125. if (ch > 0 && ch < INT_MAX/16) {
  3126. /* calc from frame_bytes and channels */
  3127. switch (id) {
  3128. case AV_CODEC_ID_ADPCM_AFC:
  3129. return frame_bytes / (9 * ch) * 16;
  3130. case AV_CODEC_ID_ADPCM_PSX:
  3131. case AV_CODEC_ID_ADPCM_DTK:
  3132. return frame_bytes / (16 * ch) * 28;
  3133. case AV_CODEC_ID_ADPCM_4XM:
  3134. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  3135. case AV_CODEC_ID_ADPCM_IMA_ISS:
  3136. return (frame_bytes - 4 * ch) * 2 / ch;
  3137. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  3138. return (frame_bytes - 4) * 2 / ch;
  3139. case AV_CODEC_ID_ADPCM_IMA_AMV:
  3140. return (frame_bytes - 8) * 2 / ch;
  3141. case AV_CODEC_ID_ADPCM_THP:
  3142. case AV_CODEC_ID_ADPCM_THP_LE:
  3143. if (extradata)
  3144. return frame_bytes * 14 / (8 * ch);
  3145. break;
  3146. case AV_CODEC_ID_ADPCM_XA:
  3147. return (frame_bytes / 128) * 224 / ch;
  3148. case AV_CODEC_ID_INTERPLAY_DPCM:
  3149. return (frame_bytes - 6 - ch) / ch;
  3150. case AV_CODEC_ID_ROQ_DPCM:
  3151. return (frame_bytes - 8) / ch;
  3152. case AV_CODEC_ID_XAN_DPCM:
  3153. return (frame_bytes - 2 * ch) / ch;
  3154. case AV_CODEC_ID_MACE3:
  3155. return 3 * frame_bytes / ch;
  3156. case AV_CODEC_ID_MACE6:
  3157. return 6 * frame_bytes / ch;
  3158. case AV_CODEC_ID_PCM_LXF:
  3159. return 2 * (frame_bytes / (5 * ch));
  3160. case AV_CODEC_ID_IAC:
  3161. case AV_CODEC_ID_IMC:
  3162. return 4 * frame_bytes / ch;
  3163. }
  3164. if (tag) {
  3165. /* calc from frame_bytes, channels, and codec_tag */
  3166. if (id == AV_CODEC_ID_SOL_DPCM) {
  3167. if (tag == 3)
  3168. return frame_bytes / ch;
  3169. else
  3170. return frame_bytes * 2 / ch;
  3171. }
  3172. }
  3173. if (ba > 0) {
  3174. /* calc from frame_bytes, channels, and block_align */
  3175. int blocks = frame_bytes / ba;
  3176. switch (id) {
  3177. case AV_CODEC_ID_ADPCM_IMA_WAV:
  3178. if (bps < 2 || bps > 5)
  3179. return 0;
  3180. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  3181. case AV_CODEC_ID_ADPCM_IMA_DK3:
  3182. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  3183. case AV_CODEC_ID_ADPCM_IMA_DK4:
  3184. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  3185. case AV_CODEC_ID_ADPCM_IMA_RAD:
  3186. return blocks * ((ba - 4 * ch) * 2 / ch);
  3187. case AV_CODEC_ID_ADPCM_MS:
  3188. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  3189. case AV_CODEC_ID_ADPCM_MTAF:
  3190. return blocks * (ba - 16) * 2 / ch;
  3191. }
  3192. }
  3193. if (bps > 0) {
  3194. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  3195. switch (id) {
  3196. case AV_CODEC_ID_PCM_DVD:
  3197. if(bps<4)
  3198. return 0;
  3199. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  3200. case AV_CODEC_ID_PCM_BLURAY:
  3201. if(bps<4)
  3202. return 0;
  3203. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  3204. case AV_CODEC_ID_S302M:
  3205. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  3206. }
  3207. }
  3208. }
  3209. }
  3210. /* Fall back on using frame_size */
  3211. if (frame_size > 1 && frame_bytes)
  3212. return frame_size;
  3213. //For WMA we currently have no other means to calculate duration thus we
  3214. //do it here by assuming CBR, which is true for all known cases.
  3215. if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
  3216. if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
  3217. return (frame_bytes * 8LL * sr) / bitrate;
  3218. }
  3219. return 0;
  3220. }
  3221. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  3222. {
  3223. return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  3224. avctx->channels, avctx->block_align,
  3225. avctx->codec_tag, avctx->bits_per_coded_sample,
  3226. avctx->bit_rate, avctx->extradata, avctx->frame_size,
  3227. frame_bytes);
  3228. }
  3229. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  3230. {
  3231. return get_audio_frame_duration(par->codec_id, par->sample_rate,
  3232. par->channels, par->block_align,
  3233. par->codec_tag, par->bits_per_coded_sample,
  3234. par->bit_rate, par->extradata, par->frame_size,
  3235. frame_bytes);
  3236. }
  3237. #if !HAVE_THREADS
  3238. int ff_thread_init(AVCodecContext *s)
  3239. {
  3240. return -1;
  3241. }
  3242. #endif
  3243. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  3244. {
  3245. unsigned int n = 0;
  3246. while (v >= 0xff) {
  3247. *s++ = 0xff;
  3248. v -= 0xff;
  3249. n++;
  3250. }
  3251. *s = v;
  3252. n++;
  3253. return n;
  3254. }
  3255. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  3256. {
  3257. int i;
  3258. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  3259. return i;
  3260. }
  3261. #if FF_API_MISSING_SAMPLE
  3262. FF_DISABLE_DEPRECATION_WARNINGS
  3263. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  3264. {
  3265. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  3266. "version to the newest one from Git. If the problem still "
  3267. "occurs, it means that your file has a feature which has not "
  3268. "been implemented.\n", feature);
  3269. if(want_sample)
  3270. av_log_ask_for_sample(avc, NULL);
  3271. }
  3272. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  3273. {
  3274. va_list argument_list;
  3275. va_start(argument_list, msg);
  3276. if (msg)
  3277. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  3278. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  3279. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  3280. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  3281. va_end(argument_list);
  3282. }
  3283. FF_ENABLE_DEPRECATION_WARNINGS
  3284. #endif /* FF_API_MISSING_SAMPLE */
  3285. static AVHWAccel *first_hwaccel = NULL;
  3286. static AVHWAccel **last_hwaccel = &first_hwaccel;
  3287. void av_register_hwaccel(AVHWAccel *hwaccel)
  3288. {
  3289. AVHWAccel **p = last_hwaccel;
  3290. hwaccel->next = NULL;
  3291. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  3292. p = &(*p)->next;
  3293. last_hwaccel = &hwaccel->next;
  3294. }
  3295. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  3296. {
  3297. return hwaccel ? hwaccel->next : first_hwaccel;
  3298. }
  3299. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  3300. {
  3301. if (lockmgr_cb) {
  3302. // There is no good way to rollback a failure to destroy the
  3303. // mutex, so we ignore failures.
  3304. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  3305. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  3306. lockmgr_cb = NULL;
  3307. codec_mutex = NULL;
  3308. avformat_mutex = NULL;
  3309. }
  3310. if (cb) {
  3311. void *new_codec_mutex = NULL;
  3312. void *new_avformat_mutex = NULL;
  3313. int err;
  3314. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  3315. return err > 0 ? AVERROR_UNKNOWN : err;
  3316. }
  3317. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  3318. // Ignore failures to destroy the newly created mutex.
  3319. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  3320. return err > 0 ? AVERROR_UNKNOWN : err;
  3321. }
  3322. lockmgr_cb = cb;
  3323. codec_mutex = new_codec_mutex;
  3324. avformat_mutex = new_avformat_mutex;
  3325. }
  3326. return 0;
  3327. }
  3328. int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
  3329. {
  3330. if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
  3331. return 0;
  3332. if (lockmgr_cb) {
  3333. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  3334. return -1;
  3335. }
  3336. if (avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, 1) != 1) {
  3337. av_log(log_ctx, AV_LOG_ERROR,
  3338. "Insufficient thread locking. At least %d threads are "
  3339. "calling avcodec_open2() at the same time right now.\n",
  3340. entangled_thread_counter);
  3341. if (!lockmgr_cb)
  3342. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  3343. ff_avcodec_locked = 1;
  3344. ff_unlock_avcodec(codec);
  3345. return AVERROR(EINVAL);
  3346. }
  3347. av_assert0(!ff_avcodec_locked);
  3348. ff_avcodec_locked = 1;
  3349. return 0;
  3350. }
  3351. int ff_unlock_avcodec(const AVCodec *codec)
  3352. {
  3353. if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
  3354. return 0;
  3355. av_assert0(ff_avcodec_locked);
  3356. ff_avcodec_locked = 0;
  3357. avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, -1);
  3358. if (lockmgr_cb) {
  3359. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  3360. return -1;
  3361. }
  3362. return 0;
  3363. }
  3364. int avpriv_lock_avformat(void)
  3365. {
  3366. if (lockmgr_cb) {
  3367. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  3368. return -1;
  3369. }
  3370. return 0;
  3371. }
  3372. int avpriv_unlock_avformat(void)
  3373. {
  3374. if (lockmgr_cb) {
  3375. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  3376. return -1;
  3377. }
  3378. return 0;
  3379. }
  3380. unsigned int avpriv_toupper4(unsigned int x)
  3381. {
  3382. return av_toupper(x & 0xFF) +
  3383. (av_toupper((x >> 8) & 0xFF) << 8) +
  3384. (av_toupper((x >> 16) & 0xFF) << 16) +
  3385. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  3386. }
  3387. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  3388. {
  3389. int ret;
  3390. dst->owner = src->owner;
  3391. ret = av_frame_ref(dst->f, src->f);
  3392. if (ret < 0)
  3393. return ret;
  3394. av_assert0(!dst->progress);
  3395. if (src->progress &&
  3396. !(dst->progress = av_buffer_ref(src->progress))) {
  3397. ff_thread_release_buffer(dst->owner, dst);
  3398. return AVERROR(ENOMEM);
  3399. }
  3400. return 0;
  3401. }
  3402. #if !HAVE_THREADS
  3403. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  3404. {
  3405. return ff_get_format(avctx, fmt);
  3406. }
  3407. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  3408. {
  3409. f->owner = avctx;
  3410. return ff_get_buffer(avctx, f->f, flags);
  3411. }
  3412. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  3413. {
  3414. if (f->f)
  3415. av_frame_unref(f->f);
  3416. }
  3417. void ff_thread_finish_setup(AVCodecContext *avctx)
  3418. {
  3419. }
  3420. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  3421. {
  3422. }
  3423. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  3424. {
  3425. }
  3426. int ff_thread_can_start_frame(AVCodecContext *avctx)
  3427. {
  3428. return 1;
  3429. }
  3430. int ff_alloc_entries(AVCodecContext *avctx, int count)
  3431. {
  3432. return 0;
  3433. }
  3434. void ff_reset_entries(AVCodecContext *avctx)
  3435. {
  3436. }
  3437. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  3438. {
  3439. }
  3440. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  3441. {
  3442. }
  3443. #endif
  3444. int avcodec_is_open(AVCodecContext *s)
  3445. {
  3446. return !!s->internal;
  3447. }
  3448. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  3449. {
  3450. int ret;
  3451. char *str;
  3452. ret = av_bprint_finalize(buf, &str);
  3453. if (ret < 0)
  3454. return ret;
  3455. if (!av_bprint_is_complete(buf)) {
  3456. av_free(str);
  3457. return AVERROR(ENOMEM);
  3458. }
  3459. avctx->extradata = str;
  3460. /* Note: the string is NUL terminated (so extradata can be read as a
  3461. * string), but the ending character is not accounted in the size (in
  3462. * binary formats you are likely not supposed to mux that character). When
  3463. * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
  3464. * zeros. */
  3465. avctx->extradata_size = buf->len;
  3466. return 0;
  3467. }
  3468. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3469. const uint8_t *end,
  3470. uint32_t *av_restrict state)
  3471. {
  3472. int i;
  3473. av_assert0(p <= end);
  3474. if (p >= end)
  3475. return end;
  3476. for (i = 0; i < 3; i++) {
  3477. uint32_t tmp = *state << 8;
  3478. *state = tmp + *(p++);
  3479. if (tmp == 0x100 || p == end)
  3480. return p;
  3481. }
  3482. while (p < end) {
  3483. if (p[-1] > 1 ) p += 3;
  3484. else if (p[-2] ) p += 2;
  3485. else if (p[-3]|(p[-1]-1)) p++;
  3486. else {
  3487. p++;
  3488. break;
  3489. }
  3490. }
  3491. p = FFMIN(p, end) - 4;
  3492. *state = AV_RB32(p);
  3493. return p + 4;
  3494. }
  3495. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  3496. {
  3497. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  3498. if (!props)
  3499. return NULL;
  3500. if (size)
  3501. *size = sizeof(*props);
  3502. props->vbv_delay = UINT64_MAX;
  3503. return props;
  3504. }
  3505. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  3506. {
  3507. AVPacketSideData *tmp;
  3508. AVCPBProperties *props;
  3509. size_t size;
  3510. props = av_cpb_properties_alloc(&size);
  3511. if (!props)
  3512. return NULL;
  3513. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  3514. if (!tmp) {
  3515. av_freep(&props);
  3516. return NULL;
  3517. }
  3518. avctx->coded_side_data = tmp;
  3519. avctx->nb_coded_side_data++;
  3520. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  3521. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  3522. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  3523. return props;
  3524. }
  3525. static void codec_parameters_reset(AVCodecParameters *par)
  3526. {
  3527. av_freep(&par->extradata);
  3528. memset(par, 0, sizeof(*par));
  3529. par->codec_type = AVMEDIA_TYPE_UNKNOWN;
  3530. par->codec_id = AV_CODEC_ID_NONE;
  3531. par->format = -1;
  3532. par->field_order = AV_FIELD_UNKNOWN;
  3533. par->color_range = AVCOL_RANGE_UNSPECIFIED;
  3534. par->color_primaries = AVCOL_PRI_UNSPECIFIED;
  3535. par->color_trc = AVCOL_TRC_UNSPECIFIED;
  3536. par->color_space = AVCOL_SPC_UNSPECIFIED;
  3537. par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  3538. par->sample_aspect_ratio = (AVRational){ 0, 1 };
  3539. par->profile = FF_PROFILE_UNKNOWN;
  3540. par->level = FF_LEVEL_UNKNOWN;
  3541. }
  3542. AVCodecParameters *avcodec_parameters_alloc(void)
  3543. {
  3544. AVCodecParameters *par = av_mallocz(sizeof(*par));
  3545. if (!par)
  3546. return NULL;
  3547. codec_parameters_reset(par);
  3548. return par;
  3549. }
  3550. void avcodec_parameters_free(AVCodecParameters **ppar)
  3551. {
  3552. AVCodecParameters *par = *ppar;
  3553. if (!par)
  3554. return;
  3555. codec_parameters_reset(par);
  3556. av_freep(ppar);
  3557. }
  3558. int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
  3559. {
  3560. codec_parameters_reset(dst);
  3561. memcpy(dst, src, sizeof(*dst));
  3562. dst->extradata = NULL;
  3563. dst->extradata_size = 0;
  3564. if (src->extradata) {
  3565. dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  3566. if (!dst->extradata)
  3567. return AVERROR(ENOMEM);
  3568. memcpy(dst->extradata, src->extradata, src->extradata_size);
  3569. dst->extradata_size = src->extradata_size;
  3570. }
  3571. return 0;
  3572. }
  3573. int avcodec_parameters_from_context(AVCodecParameters *par,
  3574. const AVCodecContext *codec)
  3575. {
  3576. codec_parameters_reset(par);
  3577. par->codec_type = codec->codec_type;
  3578. par->codec_id = codec->codec_id;
  3579. par->codec_tag = codec->codec_tag;
  3580. par->bit_rate = codec->bit_rate;
  3581. par->bits_per_coded_sample = codec->bits_per_coded_sample;
  3582. par->bits_per_raw_sample = codec->bits_per_raw_sample;
  3583. par->profile = codec->profile;
  3584. par->level = codec->level;
  3585. switch (par->codec_type) {
  3586. case AVMEDIA_TYPE_VIDEO:
  3587. par->format = codec->pix_fmt;
  3588. par->width = codec->width;
  3589. par->height = codec->height;
  3590. par->field_order = codec->field_order;
  3591. par->color_range = codec->color_range;
  3592. par->color_primaries = codec->color_primaries;
  3593. par->color_trc = codec->color_trc;
  3594. par->color_space = codec->colorspace;
  3595. par->chroma_location = codec->chroma_sample_location;
  3596. par->sample_aspect_ratio = codec->sample_aspect_ratio;
  3597. par->video_delay = codec->has_b_frames;
  3598. break;
  3599. case AVMEDIA_TYPE_AUDIO:
  3600. par->format = codec->sample_fmt;
  3601. par->channel_layout = codec->channel_layout;
  3602. par->channels = codec->channels;
  3603. par->sample_rate = codec->sample_rate;
  3604. par->block_align = codec->block_align;
  3605. par->frame_size = codec->frame_size;
  3606. par->initial_padding = codec->initial_padding;
  3607. par->trailing_padding = codec->trailing_padding;
  3608. par->seek_preroll = codec->seek_preroll;
  3609. break;
  3610. case AVMEDIA_TYPE_SUBTITLE:
  3611. par->width = codec->width;
  3612. par->height = codec->height;
  3613. break;
  3614. }
  3615. if (codec->extradata) {
  3616. par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  3617. if (!par->extradata)
  3618. return AVERROR(ENOMEM);
  3619. memcpy(par->extradata, codec->extradata, codec->extradata_size);
  3620. par->extradata_size = codec->extradata_size;
  3621. }
  3622. return 0;
  3623. }
  3624. int avcodec_parameters_to_context(AVCodecContext *codec,
  3625. const AVCodecParameters *par)
  3626. {
  3627. codec->codec_type = par->codec_type;
  3628. codec->codec_id = par->codec_id;
  3629. codec->codec_tag = par->codec_tag;
  3630. codec->bit_rate = par->bit_rate;
  3631. codec->bits_per_coded_sample = par->bits_per_coded_sample;
  3632. codec->bits_per_raw_sample = par->bits_per_raw_sample;
  3633. codec->profile = par->profile;
  3634. codec->level = par->level;
  3635. switch (par->codec_type) {
  3636. case AVMEDIA_TYPE_VIDEO:
  3637. codec->pix_fmt = par->format;
  3638. codec->width = par->width;
  3639. codec->height = par->height;
  3640. codec->field_order = par->field_order;
  3641. codec->color_range = par->color_range;
  3642. codec->color_primaries = par->color_primaries;
  3643. codec->color_trc = par->color_trc;
  3644. codec->colorspace = par->color_space;
  3645. codec->chroma_sample_location = par->chroma_location;
  3646. codec->sample_aspect_ratio = par->sample_aspect_ratio;
  3647. codec->has_b_frames = par->video_delay;
  3648. break;
  3649. case AVMEDIA_TYPE_AUDIO:
  3650. codec->sample_fmt = par->format;
  3651. codec->channel_layout = par->channel_layout;
  3652. codec->channels = par->channels;
  3653. codec->sample_rate = par->sample_rate;
  3654. codec->block_align = par->block_align;
  3655. codec->frame_size = par->frame_size;
  3656. codec->delay =
  3657. codec->initial_padding = par->initial_padding;
  3658. codec->trailing_padding = par->trailing_padding;
  3659. codec->seek_preroll = par->seek_preroll;
  3660. break;
  3661. case AVMEDIA_TYPE_SUBTITLE:
  3662. codec->width = par->width;
  3663. codec->height = par->height;
  3664. break;
  3665. }
  3666. if (par->extradata) {
  3667. av_freep(&codec->extradata);
  3668. codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  3669. if (!codec->extradata)
  3670. return AVERROR(ENOMEM);
  3671. memcpy(codec->extradata, par->extradata, par->extradata_size);
  3672. codec->extradata_size = par->extradata_size;
  3673. }
  3674. return 0;
  3675. }
  3676. int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
  3677. void **data, size_t *sei_size)
  3678. {
  3679. AVFrameSideData *side_data = NULL;
  3680. uint8_t *sei_data;
  3681. if (frame)
  3682. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
  3683. if (!side_data) {
  3684. *data = NULL;
  3685. return 0;
  3686. }
  3687. *sei_size = side_data->size + 11;
  3688. *data = av_mallocz(*sei_size + prefix_len);
  3689. if (!*data)
  3690. return AVERROR(ENOMEM);
  3691. sei_data = (uint8_t*)*data + prefix_len;
  3692. // country code
  3693. sei_data[0] = 181;
  3694. sei_data[1] = 0;
  3695. sei_data[2] = 49;
  3696. /**
  3697. * 'GA94' is standard in North America for ATSC, but hard coding
  3698. * this style may not be the right thing to do -- other formats
  3699. * do exist. This information is not available in the side_data
  3700. * so we are going with this right now.
  3701. */
  3702. AV_WL32(sei_data + 3, MKTAG('G', 'A', '9', '4'));
  3703. sei_data[7] = 3;
  3704. sei_data[8] = ((side_data->size/3) & 0x1f) | 0x40;
  3705. sei_data[9] = 0;
  3706. memcpy(sei_data + 10, side_data->data, side_data->size);
  3707. sei_data[side_data->size+10] = 255;
  3708. return 0;
  3709. }