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.

3483 lines
114KB

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