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.

4256 lines
140KB

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