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.

4305 lines
141KB

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