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.

4327 lines
142KB

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