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.

4240 lines
139KB

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