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.

4346 lines
144KB

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