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.

4387 lines
145KB

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