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.

3781 lines
124KB

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