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.

3474 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. return ret;
  785. }
  786. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  787. {
  788. AVFrame *tmp;
  789. int ret;
  790. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  791. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  792. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  793. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  794. av_frame_unref(frame);
  795. }
  796. ff_init_buffer_info(avctx, frame);
  797. if (!frame->data[0])
  798. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  799. if (av_frame_is_writable(frame))
  800. return ff_decode_frame_props(avctx, frame);
  801. tmp = av_frame_alloc();
  802. if (!tmp)
  803. return AVERROR(ENOMEM);
  804. av_frame_move_ref(tmp, frame);
  805. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  806. if (ret < 0) {
  807. av_frame_free(&tmp);
  808. return ret;
  809. }
  810. av_frame_copy(frame, tmp);
  811. av_frame_free(&tmp);
  812. return 0;
  813. }
  814. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  815. {
  816. int ret = reget_buffer_internal(avctx, frame);
  817. if (ret < 0)
  818. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  819. return ret;
  820. }
  821. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  822. {
  823. int i;
  824. for (i = 0; i < count; i++) {
  825. int r = func(c, (char *)arg + i * size);
  826. if (ret)
  827. ret[i] = r;
  828. }
  829. return 0;
  830. }
  831. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  832. {
  833. int i;
  834. for (i = 0; i < count; i++) {
  835. int r = func(c, arg, i, 0);
  836. if (ret)
  837. ret[i] = r;
  838. }
  839. return 0;
  840. }
  841. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  842. unsigned int fourcc)
  843. {
  844. while (tags->pix_fmt >= 0) {
  845. if (tags->fourcc == fourcc)
  846. return tags->pix_fmt;
  847. tags++;
  848. }
  849. return AV_PIX_FMT_NONE;
  850. }
  851. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  852. {
  853. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  854. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  855. }
  856. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  857. {
  858. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  859. ++fmt;
  860. return fmt[0];
  861. }
  862. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  863. enum AVPixelFormat pix_fmt)
  864. {
  865. AVHWAccel *hwaccel = NULL;
  866. while ((hwaccel = av_hwaccel_next(hwaccel)))
  867. if (hwaccel->id == codec_id
  868. && hwaccel->pix_fmt == pix_fmt)
  869. return hwaccel;
  870. return NULL;
  871. }
  872. static int setup_hwaccel(AVCodecContext *avctx,
  873. const enum AVPixelFormat fmt,
  874. const char *name)
  875. {
  876. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  877. int ret = 0;
  878. if (!hwa) {
  879. av_log(avctx, AV_LOG_ERROR,
  880. "Could not find an AVHWAccel for the pixel format: %s",
  881. name);
  882. return AVERROR(ENOENT);
  883. }
  884. if (hwa->capabilities & HWACCEL_CODEC_CAP_EXPERIMENTAL &&
  885. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  886. av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
  887. hwa->name);
  888. return AVERROR_PATCHWELCOME;
  889. }
  890. if (hwa->priv_data_size) {
  891. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  892. if (!avctx->internal->hwaccel_priv_data)
  893. return AVERROR(ENOMEM);
  894. }
  895. if (hwa->init) {
  896. ret = hwa->init(avctx);
  897. if (ret < 0) {
  898. av_freep(&avctx->internal->hwaccel_priv_data);
  899. return ret;
  900. }
  901. }
  902. avctx->hwaccel = hwa;
  903. return 0;
  904. }
  905. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  906. {
  907. const AVPixFmtDescriptor *desc;
  908. enum AVPixelFormat *choices;
  909. enum AVPixelFormat ret;
  910. unsigned n = 0;
  911. while (fmt[n] != AV_PIX_FMT_NONE)
  912. ++n;
  913. av_assert0(n >= 1);
  914. avctx->sw_pix_fmt = fmt[n - 1];
  915. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  916. choices = av_malloc_array(n + 1, sizeof(*choices));
  917. if (!choices)
  918. return AV_PIX_FMT_NONE;
  919. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  920. for (;;) {
  921. if (avctx->hwaccel && avctx->hwaccel->uninit)
  922. avctx->hwaccel->uninit(avctx);
  923. av_freep(&avctx->internal->hwaccel_priv_data);
  924. avctx->hwaccel = NULL;
  925. ret = avctx->get_format(avctx, choices);
  926. desc = av_pix_fmt_desc_get(ret);
  927. if (!desc) {
  928. ret = AV_PIX_FMT_NONE;
  929. break;
  930. }
  931. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  932. break;
  933. #if FF_API_CAP_VDPAU
  934. if (avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU)
  935. break;
  936. #endif
  937. if (!setup_hwaccel(avctx, ret, desc->name))
  938. break;
  939. /* Remove failed hwaccel from choices */
  940. for (n = 0; choices[n] != ret; n++)
  941. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  942. do
  943. choices[n] = choices[n + 1];
  944. while (choices[n++] != AV_PIX_FMT_NONE);
  945. }
  946. av_freep(&choices);
  947. return ret;
  948. }
  949. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  950. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  951. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  952. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  953. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  954. unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
  955. {
  956. return codec->properties;
  957. }
  958. int av_codec_get_max_lowres(const AVCodec *codec)
  959. {
  960. return codec->max_lowres;
  961. }
  962. static void get_subtitle_defaults(AVSubtitle *sub)
  963. {
  964. memset(sub, 0, sizeof(*sub));
  965. sub->pts = AV_NOPTS_VALUE;
  966. }
  967. static int64_t get_bit_rate(AVCodecContext *ctx)
  968. {
  969. int64_t bit_rate;
  970. int bits_per_sample;
  971. switch (ctx->codec_type) {
  972. case AVMEDIA_TYPE_VIDEO:
  973. case AVMEDIA_TYPE_DATA:
  974. case AVMEDIA_TYPE_SUBTITLE:
  975. case AVMEDIA_TYPE_ATTACHMENT:
  976. bit_rate = ctx->bit_rate;
  977. break;
  978. case AVMEDIA_TYPE_AUDIO:
  979. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  980. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  981. break;
  982. default:
  983. bit_rate = 0;
  984. break;
  985. }
  986. return bit_rate;
  987. }
  988. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  989. {
  990. int ret = 0;
  991. ff_unlock_avcodec(codec);
  992. ret = avcodec_open2(avctx, codec, options);
  993. ff_lock_avcodec(avctx, codec);
  994. return ret;
  995. }
  996. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  997. {
  998. int ret = 0;
  999. AVDictionary *tmp = NULL;
  1000. if (avcodec_is_open(avctx))
  1001. return 0;
  1002. if ((!codec && !avctx->codec)) {
  1003. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  1004. return AVERROR(EINVAL);
  1005. }
  1006. if ((codec && avctx->codec && codec != avctx->codec)) {
  1007. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  1008. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  1009. return AVERROR(EINVAL);
  1010. }
  1011. if (!codec)
  1012. codec = avctx->codec;
  1013. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  1014. return AVERROR(EINVAL);
  1015. if (options)
  1016. av_dict_copy(&tmp, *options, 0);
  1017. ret = ff_lock_avcodec(avctx, codec);
  1018. if (ret < 0)
  1019. return ret;
  1020. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  1021. if (!avctx->internal) {
  1022. ret = AVERROR(ENOMEM);
  1023. goto end;
  1024. }
  1025. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  1026. if (!avctx->internal->pool) {
  1027. ret = AVERROR(ENOMEM);
  1028. goto free_and_end;
  1029. }
  1030. avctx->internal->to_free = av_frame_alloc();
  1031. if (!avctx->internal->to_free) {
  1032. ret = AVERROR(ENOMEM);
  1033. goto free_and_end;
  1034. }
  1035. if (codec->priv_data_size > 0) {
  1036. if (!avctx->priv_data) {
  1037. avctx->priv_data = av_mallocz(codec->priv_data_size);
  1038. if (!avctx->priv_data) {
  1039. ret = AVERROR(ENOMEM);
  1040. goto end;
  1041. }
  1042. if (codec->priv_class) {
  1043. *(const AVClass **)avctx->priv_data = codec->priv_class;
  1044. av_opt_set_defaults(avctx->priv_data);
  1045. }
  1046. }
  1047. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  1048. goto free_and_end;
  1049. } else {
  1050. avctx->priv_data = NULL;
  1051. }
  1052. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  1053. goto free_and_end;
  1054. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  1055. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist\n", codec->name);
  1056. ret = AVERROR(EINVAL);
  1057. goto free_and_end;
  1058. }
  1059. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  1060. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1061. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  1062. if (avctx->coded_width && avctx->coded_height)
  1063. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1064. else if (avctx->width && avctx->height)
  1065. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1066. if (ret < 0)
  1067. goto free_and_end;
  1068. }
  1069. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1070. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1071. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  1072. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1073. ff_set_dimensions(avctx, 0, 0);
  1074. }
  1075. if (avctx->width > 0 && avctx->height > 0) {
  1076. if (av_image_check_sar(avctx->width, avctx->height,
  1077. avctx->sample_aspect_ratio) < 0) {
  1078. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1079. avctx->sample_aspect_ratio.num,
  1080. avctx->sample_aspect_ratio.den);
  1081. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  1082. }
  1083. }
  1084. /* if the decoder init function was already called previously,
  1085. * free the already allocated subtitle_header before overwriting it */
  1086. if (av_codec_is_decoder(codec))
  1087. av_freep(&avctx->subtitle_header);
  1088. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1089. ret = AVERROR(EINVAL);
  1090. goto free_and_end;
  1091. }
  1092. avctx->codec = codec;
  1093. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1094. avctx->codec_id == AV_CODEC_ID_NONE) {
  1095. avctx->codec_type = codec->type;
  1096. avctx->codec_id = codec->id;
  1097. }
  1098. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1099. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1100. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1101. ret = AVERROR(EINVAL);
  1102. goto free_and_end;
  1103. }
  1104. avctx->frame_number = 0;
  1105. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1106. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  1107. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1108. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1109. AVCodec *codec2;
  1110. av_log(avctx, AV_LOG_ERROR,
  1111. "The %s '%s' is experimental but experimental codecs are not enabled, "
  1112. "add '-strict %d' if you want to use it.\n",
  1113. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1114. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1115. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  1116. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1117. codec_string, codec2->name);
  1118. ret = AVERROR_EXPERIMENTAL;
  1119. goto free_and_end;
  1120. }
  1121. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1122. (!avctx->time_base.num || !avctx->time_base.den)) {
  1123. avctx->time_base.num = 1;
  1124. avctx->time_base.den = avctx->sample_rate;
  1125. }
  1126. if (!HAVE_THREADS)
  1127. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1128. if (CONFIG_FRAME_THREAD_ENCODER) {
  1129. ff_unlock_avcodec(codec); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  1130. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1131. ff_lock_avcodec(avctx, codec);
  1132. if (ret < 0)
  1133. goto free_and_end;
  1134. }
  1135. if (HAVE_THREADS
  1136. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1137. ret = ff_thread_init(avctx);
  1138. if (ret < 0) {
  1139. goto free_and_end;
  1140. }
  1141. }
  1142. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  1143. avctx->thread_count = 1;
  1144. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1145. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1146. avctx->codec->max_lowres);
  1147. ret = AVERROR(EINVAL);
  1148. goto free_and_end;
  1149. }
  1150. #if FF_API_VISMV
  1151. if (avctx->debug_mv)
  1152. av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
  1153. "see the codecview filter instead.\n");
  1154. #endif
  1155. if (av_codec_is_encoder(avctx->codec)) {
  1156. int i;
  1157. #if FF_API_CODED_FRAME
  1158. FF_DISABLE_DEPRECATION_WARNINGS
  1159. avctx->coded_frame = av_frame_alloc();
  1160. if (!avctx->coded_frame) {
  1161. ret = AVERROR(ENOMEM);
  1162. goto free_and_end;
  1163. }
  1164. FF_ENABLE_DEPRECATION_WARNINGS
  1165. #endif
  1166. if (avctx->codec->sample_fmts) {
  1167. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1168. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1169. break;
  1170. if (avctx->channels == 1 &&
  1171. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1172. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1173. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1174. break;
  1175. }
  1176. }
  1177. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1178. char buf[128];
  1179. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1180. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1181. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1182. ret = AVERROR(EINVAL);
  1183. goto free_and_end;
  1184. }
  1185. }
  1186. if (avctx->codec->pix_fmts) {
  1187. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1188. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1189. break;
  1190. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1191. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1192. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1193. char buf[128];
  1194. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1195. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1196. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1197. ret = AVERROR(EINVAL);
  1198. goto free_and_end;
  1199. }
  1200. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  1201. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
  1202. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  1203. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  1204. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  1205. avctx->color_range = AVCOL_RANGE_JPEG;
  1206. }
  1207. if (avctx->codec->supported_samplerates) {
  1208. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1209. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1210. break;
  1211. if (avctx->codec->supported_samplerates[i] == 0) {
  1212. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1213. avctx->sample_rate);
  1214. ret = AVERROR(EINVAL);
  1215. goto free_and_end;
  1216. }
  1217. }
  1218. if (avctx->sample_rate < 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. if (avctx->codec->channel_layouts) {
  1225. if (!avctx->channel_layout) {
  1226. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1227. } else {
  1228. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1229. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1230. break;
  1231. if (avctx->codec->channel_layouts[i] == 0) {
  1232. char buf[512];
  1233. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1234. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1235. ret = AVERROR(EINVAL);
  1236. goto free_and_end;
  1237. }
  1238. }
  1239. }
  1240. if (avctx->channel_layout && avctx->channels) {
  1241. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1242. if (channels != avctx->channels) {
  1243. char buf[512];
  1244. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1245. av_log(avctx, AV_LOG_ERROR,
  1246. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1247. buf, channels, avctx->channels);
  1248. ret = AVERROR(EINVAL);
  1249. goto free_and_end;
  1250. }
  1251. } else if (avctx->channel_layout) {
  1252. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1253. }
  1254. if (avctx->channels < 0) {
  1255. av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
  1256. avctx->channels);
  1257. ret = AVERROR(EINVAL);
  1258. goto free_and_end;
  1259. }
  1260. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1261. if (avctx->width <= 0 || avctx->height <= 0) {
  1262. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1263. ret = AVERROR(EINVAL);
  1264. goto free_and_end;
  1265. }
  1266. }
  1267. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1268. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1269. 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);
  1270. }
  1271. if (!avctx->rc_initial_buffer_occupancy)
  1272. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1273. }
  1274. avctx->pts_correction_num_faulty_pts =
  1275. avctx->pts_correction_num_faulty_dts = 0;
  1276. avctx->pts_correction_last_pts =
  1277. avctx->pts_correction_last_dts = INT64_MIN;
  1278. if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
  1279. && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
  1280. av_log(avctx, AV_LOG_WARNING,
  1281. "gray decoding requested but not enabled at configuration time\n");
  1282. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1283. || avctx->internal->frame_thread_encoder)) {
  1284. ret = avctx->codec->init(avctx);
  1285. if (ret < 0) {
  1286. goto free_and_end;
  1287. }
  1288. }
  1289. ret=0;
  1290. #if FF_API_AUDIOENC_DELAY
  1291. if (av_codec_is_encoder(avctx->codec))
  1292. avctx->delay = avctx->initial_padding;
  1293. #endif
  1294. if (av_codec_is_decoder(avctx->codec)) {
  1295. if (!avctx->bit_rate)
  1296. avctx->bit_rate = get_bit_rate(avctx);
  1297. /* validate channel layout from the decoder */
  1298. if (avctx->channel_layout) {
  1299. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1300. if (!avctx->channels)
  1301. avctx->channels = channels;
  1302. else if (channels != avctx->channels) {
  1303. char buf[512];
  1304. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1305. av_log(avctx, AV_LOG_WARNING,
  1306. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1307. "ignoring specified channel layout\n",
  1308. buf, channels, avctx->channels);
  1309. avctx->channel_layout = 0;
  1310. }
  1311. }
  1312. if (avctx->channels && avctx->channels < 0 ||
  1313. avctx->channels > FF_SANE_NB_CHANNELS) {
  1314. ret = AVERROR(EINVAL);
  1315. goto free_and_end;
  1316. }
  1317. if (avctx->sub_charenc) {
  1318. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1319. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1320. "supported with subtitles codecs\n");
  1321. ret = AVERROR(EINVAL);
  1322. goto free_and_end;
  1323. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1324. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1325. "subtitles character encoding will be ignored\n",
  1326. avctx->codec_descriptor->name);
  1327. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1328. } else {
  1329. /* input character encoding is set for a text based subtitle
  1330. * codec at this point */
  1331. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1332. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1333. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1334. #if CONFIG_ICONV
  1335. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1336. if (cd == (iconv_t)-1) {
  1337. ret = AVERROR(errno);
  1338. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1339. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1340. goto free_and_end;
  1341. }
  1342. iconv_close(cd);
  1343. #else
  1344. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1345. "conversion needs a libavcodec built with iconv support "
  1346. "for this codec\n");
  1347. ret = AVERROR(ENOSYS);
  1348. goto free_and_end;
  1349. #endif
  1350. }
  1351. }
  1352. }
  1353. #if FF_API_AVCTX_TIMEBASE
  1354. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1355. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  1356. #endif
  1357. }
  1358. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  1359. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  1360. }
  1361. end:
  1362. ff_unlock_avcodec(codec);
  1363. if (options) {
  1364. av_dict_free(options);
  1365. *options = tmp;
  1366. }
  1367. return ret;
  1368. free_and_end:
  1369. if (avctx->codec &&
  1370. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
  1371. avctx->codec->close(avctx);
  1372. if (codec->priv_class && codec->priv_data_size)
  1373. av_opt_free(avctx->priv_data);
  1374. av_opt_free(avctx);
  1375. #if FF_API_CODED_FRAME
  1376. FF_DISABLE_DEPRECATION_WARNINGS
  1377. av_frame_free(&avctx->coded_frame);
  1378. FF_ENABLE_DEPRECATION_WARNINGS
  1379. #endif
  1380. av_dict_free(&tmp);
  1381. av_freep(&avctx->priv_data);
  1382. if (avctx->internal) {
  1383. av_frame_free(&avctx->internal->to_free);
  1384. av_freep(&avctx->internal->pool);
  1385. }
  1386. av_freep(&avctx->internal);
  1387. avctx->codec = NULL;
  1388. goto end;
  1389. }
  1390. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
  1391. {
  1392. if (avpkt->size < 0) {
  1393. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1394. return AVERROR(EINVAL);
  1395. }
  1396. if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  1397. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1398. size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
  1399. return AVERROR(EINVAL);
  1400. }
  1401. if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
  1402. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1403. if (!avpkt->data || avpkt->size < size) {
  1404. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1405. avpkt->data = avctx->internal->byte_buffer;
  1406. avpkt->size = avctx->internal->byte_buffer_size;
  1407. }
  1408. }
  1409. if (avpkt->data) {
  1410. AVBufferRef *buf = avpkt->buf;
  1411. if (avpkt->size < size) {
  1412. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1413. return AVERROR(EINVAL);
  1414. }
  1415. av_init_packet(avpkt);
  1416. avpkt->buf = buf;
  1417. avpkt->size = size;
  1418. return 0;
  1419. } else {
  1420. int ret = av_new_packet(avpkt, size);
  1421. if (ret < 0)
  1422. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1423. return ret;
  1424. }
  1425. }
  1426. int ff_alloc_packet(AVPacket *avpkt, int size)
  1427. {
  1428. return ff_alloc_packet2(NULL, avpkt, size, 0);
  1429. }
  1430. /**
  1431. * Pad last frame with silence.
  1432. */
  1433. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1434. {
  1435. AVFrame *frame = NULL;
  1436. int ret;
  1437. if (!(frame = av_frame_alloc()))
  1438. return AVERROR(ENOMEM);
  1439. frame->format = src->format;
  1440. frame->channel_layout = src->channel_layout;
  1441. av_frame_set_channels(frame, av_frame_get_channels(src));
  1442. frame->nb_samples = s->frame_size;
  1443. ret = av_frame_get_buffer(frame, 32);
  1444. if (ret < 0)
  1445. goto fail;
  1446. ret = av_frame_copy_props(frame, src);
  1447. if (ret < 0)
  1448. goto fail;
  1449. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1450. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1451. goto fail;
  1452. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1453. frame->nb_samples - src->nb_samples,
  1454. s->channels, s->sample_fmt)) < 0)
  1455. goto fail;
  1456. *dst = frame;
  1457. return 0;
  1458. fail:
  1459. av_frame_free(&frame);
  1460. return ret;
  1461. }
  1462. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1463. AVPacket *avpkt,
  1464. const AVFrame *frame,
  1465. int *got_packet_ptr)
  1466. {
  1467. AVFrame *extended_frame = NULL;
  1468. AVFrame *padded_frame = NULL;
  1469. int ret;
  1470. AVPacket user_pkt = *avpkt;
  1471. int needs_realloc = !user_pkt.data;
  1472. *got_packet_ptr = 0;
  1473. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1474. av_free_packet(avpkt);
  1475. av_init_packet(avpkt);
  1476. return 0;
  1477. }
  1478. /* ensure that extended_data is properly set */
  1479. if (frame && !frame->extended_data) {
  1480. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1481. avctx->channels > AV_NUM_DATA_POINTERS) {
  1482. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1483. "with more than %d channels, but extended_data is not set.\n",
  1484. AV_NUM_DATA_POINTERS);
  1485. return AVERROR(EINVAL);
  1486. }
  1487. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1488. extended_frame = av_frame_alloc();
  1489. if (!extended_frame)
  1490. return AVERROR(ENOMEM);
  1491. memcpy(extended_frame, frame, sizeof(AVFrame));
  1492. extended_frame->extended_data = extended_frame->data;
  1493. frame = extended_frame;
  1494. }
  1495. /* extract audio service type metadata */
  1496. if (frame) {
  1497. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  1498. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  1499. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  1500. }
  1501. /* check for valid frame size */
  1502. if (frame) {
  1503. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  1504. if (frame->nb_samples > avctx->frame_size) {
  1505. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1506. ret = AVERROR(EINVAL);
  1507. goto end;
  1508. }
  1509. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1510. if (frame->nb_samples < avctx->frame_size &&
  1511. !avctx->internal->last_audio_frame) {
  1512. ret = pad_last_frame(avctx, &padded_frame, frame);
  1513. if (ret < 0)
  1514. goto end;
  1515. frame = padded_frame;
  1516. avctx->internal->last_audio_frame = 1;
  1517. }
  1518. if (frame->nb_samples != avctx->frame_size) {
  1519. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1520. ret = AVERROR(EINVAL);
  1521. goto end;
  1522. }
  1523. }
  1524. }
  1525. av_assert0(avctx->codec->encode2);
  1526. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1527. if (!ret) {
  1528. if (*got_packet_ptr) {
  1529. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  1530. if (avpkt->pts == AV_NOPTS_VALUE)
  1531. avpkt->pts = frame->pts;
  1532. if (!avpkt->duration)
  1533. avpkt->duration = ff_samples_to_time_base(avctx,
  1534. frame->nb_samples);
  1535. }
  1536. avpkt->dts = avpkt->pts;
  1537. } else {
  1538. avpkt->size = 0;
  1539. }
  1540. }
  1541. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1542. needs_realloc = 0;
  1543. if (user_pkt.data) {
  1544. if (user_pkt.size >= avpkt->size) {
  1545. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1546. } else {
  1547. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1548. avpkt->size = user_pkt.size;
  1549. ret = -1;
  1550. }
  1551. avpkt->buf = user_pkt.buf;
  1552. avpkt->data = user_pkt.data;
  1553. } else {
  1554. if (av_dup_packet(avpkt) < 0) {
  1555. ret = AVERROR(ENOMEM);
  1556. }
  1557. }
  1558. }
  1559. if (!ret) {
  1560. if (needs_realloc && avpkt->data) {
  1561. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  1562. if (ret >= 0)
  1563. avpkt->data = avpkt->buf->data;
  1564. }
  1565. avctx->frame_number++;
  1566. }
  1567. if (ret < 0 || !*got_packet_ptr) {
  1568. av_free_packet(avpkt);
  1569. av_init_packet(avpkt);
  1570. goto end;
  1571. }
  1572. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1573. * this needs to be moved to the encoders, but for now we can do it
  1574. * here to simplify things */
  1575. avpkt->flags |= AV_PKT_FLAG_KEY;
  1576. end:
  1577. av_frame_free(&padded_frame);
  1578. av_free(extended_frame);
  1579. #if FF_API_AUDIOENC_DELAY
  1580. avctx->delay = avctx->initial_padding;
  1581. #endif
  1582. return ret;
  1583. }
  1584. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1585. AVPacket *avpkt,
  1586. const AVFrame *frame,
  1587. int *got_packet_ptr)
  1588. {
  1589. int ret;
  1590. AVPacket user_pkt = *avpkt;
  1591. int needs_realloc = !user_pkt.data;
  1592. *got_packet_ptr = 0;
  1593. if(CONFIG_FRAME_THREAD_ENCODER &&
  1594. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1595. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1596. if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  1597. avctx->stats_out[0] = '\0';
  1598. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1599. av_free_packet(avpkt);
  1600. av_init_packet(avpkt);
  1601. avpkt->size = 0;
  1602. return 0;
  1603. }
  1604. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1605. return AVERROR(EINVAL);
  1606. if (frame && frame->format == AV_PIX_FMT_NONE)
  1607. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  1608. if (frame && (frame->width == 0 || frame->height == 0))
  1609. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  1610. av_assert0(avctx->codec->encode2);
  1611. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1612. av_assert0(ret <= 0);
  1613. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1614. needs_realloc = 0;
  1615. if (user_pkt.data) {
  1616. if (user_pkt.size >= avpkt->size) {
  1617. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1618. } else {
  1619. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1620. avpkt->size = user_pkt.size;
  1621. ret = -1;
  1622. }
  1623. avpkt->buf = user_pkt.buf;
  1624. avpkt->data = user_pkt.data;
  1625. } else {
  1626. if (av_dup_packet(avpkt) < 0) {
  1627. ret = AVERROR(ENOMEM);
  1628. }
  1629. }
  1630. }
  1631. if (!ret) {
  1632. if (!*got_packet_ptr)
  1633. avpkt->size = 0;
  1634. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1635. avpkt->pts = avpkt->dts = frame->pts;
  1636. if (needs_realloc && avpkt->data) {
  1637. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  1638. if (ret >= 0)
  1639. avpkt->data = avpkt->buf->data;
  1640. }
  1641. avctx->frame_number++;
  1642. }
  1643. if (ret < 0 || !*got_packet_ptr)
  1644. av_free_packet(avpkt);
  1645. emms_c();
  1646. return ret;
  1647. }
  1648. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1649. const AVSubtitle *sub)
  1650. {
  1651. int ret;
  1652. if (sub->start_display_time) {
  1653. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1654. return -1;
  1655. }
  1656. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1657. avctx->frame_number++;
  1658. return ret;
  1659. }
  1660. /**
  1661. * Attempt to guess proper monotonic timestamps for decoded video frames
  1662. * which might have incorrect times. Input timestamps may wrap around, in
  1663. * which case the output will as well.
  1664. *
  1665. * @param pts the pts field of the decoded AVPacket, as passed through
  1666. * AVFrame.pkt_pts
  1667. * @param dts the dts field of the decoded AVPacket
  1668. * @return one of the input values, may be AV_NOPTS_VALUE
  1669. */
  1670. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1671. int64_t reordered_pts, int64_t dts)
  1672. {
  1673. int64_t pts = AV_NOPTS_VALUE;
  1674. if (dts != AV_NOPTS_VALUE) {
  1675. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1676. ctx->pts_correction_last_dts = dts;
  1677. } else if (reordered_pts != AV_NOPTS_VALUE)
  1678. ctx->pts_correction_last_dts = reordered_pts;
  1679. if (reordered_pts != AV_NOPTS_VALUE) {
  1680. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1681. ctx->pts_correction_last_pts = reordered_pts;
  1682. } else if(dts != AV_NOPTS_VALUE)
  1683. ctx->pts_correction_last_pts = dts;
  1684. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1685. && reordered_pts != AV_NOPTS_VALUE)
  1686. pts = reordered_pts;
  1687. else
  1688. pts = dts;
  1689. return pts;
  1690. }
  1691. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1692. {
  1693. int size = 0, ret;
  1694. const uint8_t *data;
  1695. uint32_t flags;
  1696. int64_t val;
  1697. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1698. if (!data)
  1699. return 0;
  1700. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  1701. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1702. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1703. return AVERROR(EINVAL);
  1704. }
  1705. if (size < 4)
  1706. goto fail;
  1707. flags = bytestream_get_le32(&data);
  1708. size -= 4;
  1709. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1710. if (size < 4)
  1711. goto fail;
  1712. val = bytestream_get_le32(&data);
  1713. if (val <= 0 || val > INT_MAX) {
  1714. av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
  1715. return AVERROR_INVALIDDATA;
  1716. }
  1717. avctx->channels = val;
  1718. size -= 4;
  1719. }
  1720. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1721. if (size < 8)
  1722. goto fail;
  1723. avctx->channel_layout = bytestream_get_le64(&data);
  1724. size -= 8;
  1725. }
  1726. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1727. if (size < 4)
  1728. goto fail;
  1729. val = bytestream_get_le32(&data);
  1730. if (val <= 0 || val > INT_MAX) {
  1731. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
  1732. return AVERROR_INVALIDDATA;
  1733. }
  1734. avctx->sample_rate = val;
  1735. size -= 4;
  1736. }
  1737. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1738. if (size < 8)
  1739. goto fail;
  1740. avctx->width = bytestream_get_le32(&data);
  1741. avctx->height = bytestream_get_le32(&data);
  1742. size -= 8;
  1743. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1744. if (ret < 0)
  1745. return ret;
  1746. }
  1747. return 0;
  1748. fail:
  1749. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1750. return AVERROR_INVALIDDATA;
  1751. }
  1752. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1753. {
  1754. int ret;
  1755. /* move the original frame to our backup */
  1756. av_frame_unref(avci->to_free);
  1757. av_frame_move_ref(avci->to_free, frame);
  1758. /* now copy everything except the AVBufferRefs back
  1759. * note that we make a COPY of the side data, so calling av_frame_free() on
  1760. * the caller's frame will work properly */
  1761. ret = av_frame_copy_props(frame, avci->to_free);
  1762. if (ret < 0)
  1763. return ret;
  1764. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1765. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1766. if (avci->to_free->extended_data != avci->to_free->data) {
  1767. int planes = av_frame_get_channels(avci->to_free);
  1768. int size = planes * sizeof(*frame->extended_data);
  1769. if (!size) {
  1770. av_frame_unref(frame);
  1771. return AVERROR_BUG;
  1772. }
  1773. frame->extended_data = av_malloc(size);
  1774. if (!frame->extended_data) {
  1775. av_frame_unref(frame);
  1776. return AVERROR(ENOMEM);
  1777. }
  1778. memcpy(frame->extended_data, avci->to_free->extended_data,
  1779. size);
  1780. } else
  1781. frame->extended_data = frame->data;
  1782. frame->format = avci->to_free->format;
  1783. frame->width = avci->to_free->width;
  1784. frame->height = avci->to_free->height;
  1785. frame->channel_layout = avci->to_free->channel_layout;
  1786. frame->nb_samples = avci->to_free->nb_samples;
  1787. av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
  1788. return 0;
  1789. }
  1790. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1791. int *got_picture_ptr,
  1792. const AVPacket *avpkt)
  1793. {
  1794. AVCodecInternal *avci = avctx->internal;
  1795. int ret;
  1796. // copy to ensure we do not change avpkt
  1797. AVPacket tmp = *avpkt;
  1798. if (!avctx->codec)
  1799. return AVERROR(EINVAL);
  1800. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1801. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1802. return AVERROR(EINVAL);
  1803. }
  1804. *got_picture_ptr = 0;
  1805. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1806. return AVERROR(EINVAL);
  1807. av_frame_unref(picture);
  1808. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
  1809. (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1810. int did_split = av_packet_split_side_data(&tmp);
  1811. ret = apply_param_change(avctx, &tmp);
  1812. if (ret < 0) {
  1813. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1814. if (avctx->err_recognition & AV_EF_EXPLODE)
  1815. goto fail;
  1816. }
  1817. avctx->internal->pkt = &tmp;
  1818. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1819. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1820. &tmp);
  1821. else {
  1822. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1823. &tmp);
  1824. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  1825. picture->pkt_dts = avpkt->dts;
  1826. if(!avctx->has_b_frames){
  1827. av_frame_set_pkt_pos(picture, avpkt->pos);
  1828. }
  1829. //FIXME these should be under if(!avctx->has_b_frames)
  1830. /* get_buffer is supposed to set frame parameters */
  1831. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  1832. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1833. if (!picture->width) picture->width = avctx->width;
  1834. if (!picture->height) picture->height = avctx->height;
  1835. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1836. }
  1837. }
  1838. fail:
  1839. emms_c(); //needed to avoid an emms_c() call before every return;
  1840. avctx->internal->pkt = NULL;
  1841. if (did_split) {
  1842. av_packet_free_side_data(&tmp);
  1843. if(ret == tmp.size)
  1844. ret = avpkt->size;
  1845. }
  1846. if (*got_picture_ptr) {
  1847. if (!avctx->refcounted_frames) {
  1848. int err = unrefcount_frame(avci, picture);
  1849. if (err < 0)
  1850. return err;
  1851. }
  1852. avctx->frame_number++;
  1853. av_frame_set_best_effort_timestamp(picture,
  1854. guess_correct_pts(avctx,
  1855. picture->pkt_pts,
  1856. picture->pkt_dts));
  1857. } else
  1858. av_frame_unref(picture);
  1859. } else
  1860. ret = 0;
  1861. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1862. * make sure it's set correctly */
  1863. av_assert0(!picture->extended_data || picture->extended_data == picture->data);
  1864. #if FF_API_AVCTX_TIMEBASE
  1865. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1866. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  1867. #endif
  1868. return ret;
  1869. }
  1870. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1871. AVFrame *frame,
  1872. int *got_frame_ptr,
  1873. const AVPacket *avpkt)
  1874. {
  1875. AVCodecInternal *avci = avctx->internal;
  1876. int ret = 0;
  1877. *got_frame_ptr = 0;
  1878. if (!avpkt->data && avpkt->size) {
  1879. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1880. return AVERROR(EINVAL);
  1881. }
  1882. if (!avctx->codec)
  1883. return AVERROR(EINVAL);
  1884. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1885. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1886. return AVERROR(EINVAL);
  1887. }
  1888. av_frame_unref(frame);
  1889. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1890. uint8_t *side;
  1891. int side_size;
  1892. uint32_t discard_padding = 0;
  1893. uint8_t skip_reason = 0;
  1894. uint8_t discard_reason = 0;
  1895. // copy to ensure we do not change avpkt
  1896. AVPacket tmp = *avpkt;
  1897. int did_split = av_packet_split_side_data(&tmp);
  1898. ret = apply_param_change(avctx, &tmp);
  1899. if (ret < 0) {
  1900. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1901. if (avctx->err_recognition & AV_EF_EXPLODE)
  1902. goto fail;
  1903. }
  1904. avctx->internal->pkt = &tmp;
  1905. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1906. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  1907. else {
  1908. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1909. av_assert0(ret <= tmp.size);
  1910. frame->pkt_dts = avpkt->dts;
  1911. }
  1912. if (ret >= 0 && *got_frame_ptr) {
  1913. avctx->frame_number++;
  1914. av_frame_set_best_effort_timestamp(frame,
  1915. guess_correct_pts(avctx,
  1916. frame->pkt_pts,
  1917. frame->pkt_dts));
  1918. if (frame->format == AV_SAMPLE_FMT_NONE)
  1919. frame->format = avctx->sample_fmt;
  1920. if (!frame->channel_layout)
  1921. frame->channel_layout = avctx->channel_layout;
  1922. if (!av_frame_get_channels(frame))
  1923. av_frame_set_channels(frame, avctx->channels);
  1924. if (!frame->sample_rate)
  1925. frame->sample_rate = avctx->sample_rate;
  1926. }
  1927. side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1928. if(side && side_size>=10) {
  1929. avctx->internal->skip_samples = AV_RL32(side);
  1930. discard_padding = AV_RL32(side + 4);
  1931. av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
  1932. avctx->internal->skip_samples, (int)discard_padding);
  1933. skip_reason = AV_RL8(side + 8);
  1934. discard_reason = AV_RL8(side + 9);
  1935. }
  1936. if (avctx->internal->skip_samples && *got_frame_ptr &&
  1937. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  1938. if(frame->nb_samples <= avctx->internal->skip_samples){
  1939. *got_frame_ptr = 0;
  1940. avctx->internal->skip_samples -= frame->nb_samples;
  1941. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1942. avctx->internal->skip_samples);
  1943. } else {
  1944. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1945. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1946. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1947. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1948. (AVRational){1, avctx->sample_rate},
  1949. avctx->pkt_timebase);
  1950. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1951. frame->pkt_pts += diff_ts;
  1952. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1953. frame->pkt_dts += diff_ts;
  1954. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1955. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1956. } else {
  1957. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1958. }
  1959. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1960. avctx->internal->skip_samples, frame->nb_samples);
  1961. frame->nb_samples -= avctx->internal->skip_samples;
  1962. avctx->internal->skip_samples = 0;
  1963. }
  1964. }
  1965. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
  1966. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  1967. if (discard_padding == frame->nb_samples) {
  1968. *got_frame_ptr = 0;
  1969. } else {
  1970. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1971. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  1972. (AVRational){1, avctx->sample_rate},
  1973. avctx->pkt_timebase);
  1974. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1975. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1976. } else {
  1977. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  1978. }
  1979. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  1980. (int)discard_padding, frame->nb_samples);
  1981. frame->nb_samples -= discard_padding;
  1982. }
  1983. }
  1984. if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
  1985. AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
  1986. if (fside) {
  1987. AV_WL32(fside->data, avctx->internal->skip_samples);
  1988. AV_WL32(fside->data + 4, discard_padding);
  1989. AV_WL8(fside->data + 8, skip_reason);
  1990. AV_WL8(fside->data + 9, discard_reason);
  1991. avctx->internal->skip_samples = 0;
  1992. }
  1993. }
  1994. fail:
  1995. avctx->internal->pkt = NULL;
  1996. if (did_split) {
  1997. av_packet_free_side_data(&tmp);
  1998. if(ret == tmp.size)
  1999. ret = avpkt->size;
  2000. }
  2001. if (ret >= 0 && *got_frame_ptr) {
  2002. if (!avctx->refcounted_frames) {
  2003. int err = unrefcount_frame(avci, frame);
  2004. if (err < 0)
  2005. return err;
  2006. }
  2007. } else
  2008. av_frame_unref(frame);
  2009. }
  2010. return ret;
  2011. }
  2012. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2013. static int recode_subtitle(AVCodecContext *avctx,
  2014. AVPacket *outpkt, const AVPacket *inpkt)
  2015. {
  2016. #if CONFIG_ICONV
  2017. iconv_t cd = (iconv_t)-1;
  2018. int ret = 0;
  2019. char *inb, *outb;
  2020. size_t inl, outl;
  2021. AVPacket tmp;
  2022. #endif
  2023. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2024. return 0;
  2025. #if CONFIG_ICONV
  2026. cd = iconv_open("UTF-8", avctx->sub_charenc);
  2027. av_assert0(cd != (iconv_t)-1);
  2028. inb = inpkt->data;
  2029. inl = inpkt->size;
  2030. if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
  2031. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2032. ret = AVERROR(ENOMEM);
  2033. goto end;
  2034. }
  2035. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2036. if (ret < 0)
  2037. goto end;
  2038. outpkt->buf = tmp.buf;
  2039. outpkt->data = tmp.data;
  2040. outpkt->size = tmp.size;
  2041. outb = outpkt->data;
  2042. outl = outpkt->size;
  2043. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2044. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2045. outl >= outpkt->size || inl != 0) {
  2046. ret = FFMIN(AVERROR(errno), -1);
  2047. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2048. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2049. av_free_packet(&tmp);
  2050. goto end;
  2051. }
  2052. outpkt->size -= outl;
  2053. memset(outpkt->data + outpkt->size, 0, outl);
  2054. end:
  2055. if (cd != (iconv_t)-1)
  2056. iconv_close(cd);
  2057. return ret;
  2058. #else
  2059. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  2060. return AVERROR(EINVAL);
  2061. #endif
  2062. }
  2063. static int utf8_check(const uint8_t *str)
  2064. {
  2065. const uint8_t *byte;
  2066. uint32_t codepoint, min;
  2067. while (*str) {
  2068. byte = str;
  2069. GET_UTF8(codepoint, *(byte++), return 0;);
  2070. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2071. 1 << (5 * (byte - str) - 4);
  2072. if (codepoint < min || codepoint >= 0x110000 ||
  2073. codepoint == 0xFFFE /* BOM */ ||
  2074. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2075. return 0;
  2076. str = byte;
  2077. }
  2078. return 1;
  2079. }
  2080. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2081. int *got_sub_ptr,
  2082. AVPacket *avpkt)
  2083. {
  2084. int i, ret = 0;
  2085. if (!avpkt->data && avpkt->size) {
  2086. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2087. return AVERROR(EINVAL);
  2088. }
  2089. if (!avctx->codec)
  2090. return AVERROR(EINVAL);
  2091. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2092. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2093. return AVERROR(EINVAL);
  2094. }
  2095. *got_sub_ptr = 0;
  2096. get_subtitle_defaults(sub);
  2097. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  2098. AVPacket pkt_recoded;
  2099. AVPacket tmp = *avpkt;
  2100. int did_split = av_packet_split_side_data(&tmp);
  2101. //apply_param_change(avctx, &tmp);
  2102. if (did_split) {
  2103. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  2104. * proper padding.
  2105. * If the side data is smaller than the buffer padding size, the
  2106. * remaining bytes should have already been filled with zeros by the
  2107. * original packet allocation anyway. */
  2108. memset(tmp.data + tmp.size, 0,
  2109. FFMIN(avpkt->size - tmp.size, AV_INPUT_BUFFER_PADDING_SIZE));
  2110. }
  2111. pkt_recoded = tmp;
  2112. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2113. if (ret < 0) {
  2114. *got_sub_ptr = 0;
  2115. } else {
  2116. avctx->internal->pkt = &pkt_recoded;
  2117. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  2118. sub->pts = av_rescale_q(avpkt->pts,
  2119. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2120. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2121. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2122. !!*got_sub_ptr >= !!sub->num_rects);
  2123. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2124. avctx->pkt_timebase.num) {
  2125. AVRational ms = { 1, 1000 };
  2126. sub->end_display_time = av_rescale_q(avpkt->duration,
  2127. avctx->pkt_timebase, ms);
  2128. }
  2129. for (i = 0; i < sub->num_rects; i++) {
  2130. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2131. av_log(avctx, AV_LOG_ERROR,
  2132. "Invalid UTF-8 in decoded subtitles text; "
  2133. "maybe missing -sub_charenc option\n");
  2134. avsubtitle_free(sub);
  2135. return AVERROR_INVALIDDATA;
  2136. }
  2137. }
  2138. if (tmp.data != pkt_recoded.data) { // did we recode?
  2139. /* prevent from destroying side data from original packet */
  2140. pkt_recoded.side_data = NULL;
  2141. pkt_recoded.side_data_elems = 0;
  2142. av_free_packet(&pkt_recoded);
  2143. }
  2144. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2145. sub->format = 0;
  2146. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2147. sub->format = 1;
  2148. avctx->internal->pkt = NULL;
  2149. }
  2150. if (did_split) {
  2151. av_packet_free_side_data(&tmp);
  2152. if(ret == tmp.size)
  2153. ret = avpkt->size;
  2154. }
  2155. if (*got_sub_ptr)
  2156. avctx->frame_number++;
  2157. }
  2158. return ret;
  2159. }
  2160. void avsubtitle_free(AVSubtitle *sub)
  2161. {
  2162. int i;
  2163. for (i = 0; i < sub->num_rects; i++) {
  2164. av_freep(&sub->rects[i]->data[0]);
  2165. av_freep(&sub->rects[i]->data[1]);
  2166. av_freep(&sub->rects[i]->data[2]);
  2167. av_freep(&sub->rects[i]->data[3]);
  2168. av_freep(&sub->rects[i]->text);
  2169. av_freep(&sub->rects[i]->ass);
  2170. av_freep(&sub->rects[i]);
  2171. }
  2172. av_freep(&sub->rects);
  2173. memset(sub, 0, sizeof(AVSubtitle));
  2174. }
  2175. av_cold int avcodec_close(AVCodecContext *avctx)
  2176. {
  2177. if (!avctx)
  2178. return 0;
  2179. if (avcodec_is_open(avctx)) {
  2180. FramePool *pool = avctx->internal->pool;
  2181. int i;
  2182. if (CONFIG_FRAME_THREAD_ENCODER &&
  2183. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2184. ff_frame_thread_encoder_free(avctx);
  2185. }
  2186. if (HAVE_THREADS && avctx->internal->thread_ctx)
  2187. ff_thread_free(avctx);
  2188. if (avctx->codec && avctx->codec->close)
  2189. avctx->codec->close(avctx);
  2190. avctx->internal->byte_buffer_size = 0;
  2191. av_freep(&avctx->internal->byte_buffer);
  2192. av_frame_free(&avctx->internal->to_free);
  2193. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2194. av_buffer_pool_uninit(&pool->pools[i]);
  2195. av_freep(&avctx->internal->pool);
  2196. if (avctx->hwaccel && avctx->hwaccel->uninit)
  2197. avctx->hwaccel->uninit(avctx);
  2198. av_freep(&avctx->internal->hwaccel_priv_data);
  2199. av_freep(&avctx->internal);
  2200. }
  2201. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2202. av_opt_free(avctx->priv_data);
  2203. av_opt_free(avctx);
  2204. av_freep(&avctx->priv_data);
  2205. if (av_codec_is_encoder(avctx->codec)) {
  2206. av_freep(&avctx->extradata);
  2207. #if FF_API_CODED_FRAME
  2208. FF_DISABLE_DEPRECATION_WARNINGS
  2209. av_frame_free(&avctx->coded_frame);
  2210. FF_ENABLE_DEPRECATION_WARNINGS
  2211. #endif
  2212. }
  2213. avctx->codec = NULL;
  2214. avctx->active_thread_type = 0;
  2215. return 0;
  2216. }
  2217. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2218. {
  2219. switch(id){
  2220. //This is for future deprecatec codec ids, its empty since
  2221. //last major bump but will fill up again over time, please don't remove it
  2222. default : return id;
  2223. }
  2224. }
  2225. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2226. {
  2227. AVCodec *p, *experimental = NULL;
  2228. p = first_avcodec;
  2229. id= remap_deprecated_codec_id(id);
  2230. while (p) {
  2231. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2232. p->id == id) {
  2233. if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
  2234. experimental = p;
  2235. } else
  2236. return p;
  2237. }
  2238. p = p->next;
  2239. }
  2240. return experimental;
  2241. }
  2242. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2243. {
  2244. return find_encdec(id, 1);
  2245. }
  2246. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2247. {
  2248. AVCodec *p;
  2249. if (!name)
  2250. return NULL;
  2251. p = first_avcodec;
  2252. while (p) {
  2253. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2254. return p;
  2255. p = p->next;
  2256. }
  2257. return NULL;
  2258. }
  2259. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2260. {
  2261. return find_encdec(id, 0);
  2262. }
  2263. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2264. {
  2265. AVCodec *p;
  2266. if (!name)
  2267. return NULL;
  2268. p = first_avcodec;
  2269. while (p) {
  2270. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2271. return p;
  2272. p = p->next;
  2273. }
  2274. return NULL;
  2275. }
  2276. const char *avcodec_get_name(enum AVCodecID id)
  2277. {
  2278. const AVCodecDescriptor *cd;
  2279. AVCodec *codec;
  2280. if (id == AV_CODEC_ID_NONE)
  2281. return "none";
  2282. cd = avcodec_descriptor_get(id);
  2283. if (cd)
  2284. return cd->name;
  2285. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2286. codec = avcodec_find_decoder(id);
  2287. if (codec)
  2288. return codec->name;
  2289. codec = avcodec_find_encoder(id);
  2290. if (codec)
  2291. return codec->name;
  2292. return "unknown_codec";
  2293. }
  2294. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2295. {
  2296. int i, len, ret = 0;
  2297. #define TAG_PRINT(x) \
  2298. (((x) >= '0' && (x) <= '9') || \
  2299. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2300. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2301. for (i = 0; i < 4; i++) {
  2302. len = snprintf(buf, buf_size,
  2303. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2304. buf += len;
  2305. buf_size = buf_size > len ? buf_size - len : 0;
  2306. ret += len;
  2307. codec_tag >>= 8;
  2308. }
  2309. return ret;
  2310. }
  2311. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2312. {
  2313. const char *codec_type;
  2314. const char *codec_name;
  2315. const char *profile = NULL;
  2316. const AVCodec *p;
  2317. int64_t bitrate;
  2318. int new_line = 0;
  2319. AVRational display_aspect_ratio;
  2320. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  2321. if (!buf || buf_size <= 0)
  2322. return;
  2323. codec_type = av_get_media_type_string(enc->codec_type);
  2324. codec_name = avcodec_get_name(enc->codec_id);
  2325. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2326. if (enc->codec)
  2327. p = enc->codec;
  2328. else
  2329. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2330. avcodec_find_decoder(enc->codec_id);
  2331. if (p)
  2332. profile = av_get_profile_name(p, enc->profile);
  2333. }
  2334. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2335. codec_name);
  2336. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2337. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2338. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2339. if (profile)
  2340. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2341. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  2342. && av_log_get_level() >= AV_LOG_VERBOSE
  2343. && enc->refs)
  2344. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2345. ", %d reference frame%s",
  2346. enc->refs, enc->refs > 1 ? "s" : "");
  2347. if (enc->codec_tag) {
  2348. char tag_buf[32];
  2349. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2350. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2351. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2352. }
  2353. switch (enc->codec_type) {
  2354. case AVMEDIA_TYPE_VIDEO:
  2355. {
  2356. char detail[256] = "(";
  2357. av_strlcat(buf, separator, buf_size);
  2358. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2359. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  2360. av_get_pix_fmt_name(enc->pix_fmt));
  2361. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  2362. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  2363. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2364. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2365. av_strlcatf(detail, sizeof(detail), "%s, ",
  2366. av_color_range_name(enc->color_range));
  2367. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  2368. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  2369. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  2370. if (enc->colorspace != (int)enc->color_primaries ||
  2371. enc->colorspace != (int)enc->color_trc) {
  2372. new_line = 1;
  2373. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  2374. av_color_space_name(enc->colorspace),
  2375. av_color_primaries_name(enc->color_primaries),
  2376. av_color_transfer_name(enc->color_trc));
  2377. } else
  2378. av_strlcatf(detail, sizeof(detail), "%s, ",
  2379. av_get_colorspace_name(enc->colorspace));
  2380. }
  2381. if (av_log_get_level() >= AV_LOG_DEBUG &&
  2382. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  2383. av_strlcatf(detail, sizeof(detail), "%s, ",
  2384. av_chroma_location_name(enc->chroma_sample_location));
  2385. if (strlen(detail) > 1) {
  2386. detail[strlen(detail) - 2] = 0;
  2387. av_strlcatf(buf, buf_size, "%s)", detail);
  2388. }
  2389. }
  2390. if (enc->width) {
  2391. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  2392. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2393. "%dx%d",
  2394. enc->width, enc->height);
  2395. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  2396. (enc->width != enc->coded_width ||
  2397. enc->height != enc->coded_height))
  2398. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2399. " (%dx%d)", enc->coded_width, enc->coded_height);
  2400. if (enc->sample_aspect_ratio.num) {
  2401. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2402. enc->width * enc->sample_aspect_ratio.num,
  2403. enc->height * enc->sample_aspect_ratio.den,
  2404. 1024 * 1024);
  2405. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2406. " [SAR %d:%d DAR %d:%d]",
  2407. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2408. display_aspect_ratio.num, display_aspect_ratio.den);
  2409. }
  2410. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2411. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2412. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2413. ", %d/%d",
  2414. enc->time_base.num / g, enc->time_base.den / g);
  2415. }
  2416. }
  2417. if (encode) {
  2418. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2419. ", q=%d-%d", enc->qmin, enc->qmax);
  2420. } else {
  2421. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  2422. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2423. ", Closed Captions");
  2424. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  2425. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2426. ", lossless");
  2427. }
  2428. break;
  2429. case AVMEDIA_TYPE_AUDIO:
  2430. av_strlcat(buf, separator, buf_size);
  2431. if (enc->sample_rate) {
  2432. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2433. "%d Hz, ", enc->sample_rate);
  2434. }
  2435. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2436. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2437. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2438. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2439. }
  2440. if ( enc->bits_per_raw_sample > 0
  2441. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  2442. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2443. " (%d bit)", enc->bits_per_raw_sample);
  2444. break;
  2445. case AVMEDIA_TYPE_DATA:
  2446. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2447. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2448. if (g)
  2449. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2450. ", %d/%d",
  2451. enc->time_base.num / g, enc->time_base.den / g);
  2452. }
  2453. break;
  2454. case AVMEDIA_TYPE_SUBTITLE:
  2455. if (enc->width)
  2456. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2457. ", %dx%d", enc->width, enc->height);
  2458. break;
  2459. default:
  2460. return;
  2461. }
  2462. if (encode) {
  2463. if (enc->flags & AV_CODEC_FLAG_PASS1)
  2464. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2465. ", pass 1");
  2466. if (enc->flags & AV_CODEC_FLAG_PASS2)
  2467. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2468. ", pass 2");
  2469. }
  2470. bitrate = get_bit_rate(enc);
  2471. if (bitrate != 0) {
  2472. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2473. ", %"PRId64" kb/s", bitrate / 1000);
  2474. } else if (enc->rc_max_rate > 0) {
  2475. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2476. ", max. %"PRId64" kb/s", (int64_t)enc->rc_max_rate / 1000);
  2477. }
  2478. }
  2479. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2480. {
  2481. const AVProfile *p;
  2482. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2483. return NULL;
  2484. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2485. if (p->profile == profile)
  2486. return p->name;
  2487. return NULL;
  2488. }
  2489. unsigned avcodec_version(void)
  2490. {
  2491. // av_assert0(AV_CODEC_ID_V410==164);
  2492. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2493. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2494. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2495. av_assert0(AV_CODEC_ID_SRT==94216);
  2496. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2497. return LIBAVCODEC_VERSION_INT;
  2498. }
  2499. const char *avcodec_configuration(void)
  2500. {
  2501. return FFMPEG_CONFIGURATION;
  2502. }
  2503. const char *avcodec_license(void)
  2504. {
  2505. #define LICENSE_PREFIX "libavcodec license: "
  2506. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2507. }
  2508. void avcodec_flush_buffers(AVCodecContext *avctx)
  2509. {
  2510. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2511. ff_thread_flush(avctx);
  2512. else if (avctx->codec->flush)
  2513. avctx->codec->flush(avctx);
  2514. avctx->pts_correction_last_pts =
  2515. avctx->pts_correction_last_dts = INT64_MIN;
  2516. if (!avctx->refcounted_frames)
  2517. av_frame_unref(avctx->internal->to_free);
  2518. }
  2519. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2520. {
  2521. switch (codec_id) {
  2522. case AV_CODEC_ID_8SVX_EXP:
  2523. case AV_CODEC_ID_8SVX_FIB:
  2524. case AV_CODEC_ID_ADPCM_CT:
  2525. case AV_CODEC_ID_ADPCM_IMA_APC:
  2526. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2527. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2528. case AV_CODEC_ID_ADPCM_IMA_WS:
  2529. case AV_CODEC_ID_ADPCM_G722:
  2530. case AV_CODEC_ID_ADPCM_YAMAHA:
  2531. return 4;
  2532. case AV_CODEC_ID_DSD_LSBF:
  2533. case AV_CODEC_ID_DSD_MSBF:
  2534. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  2535. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  2536. case AV_CODEC_ID_PCM_ALAW:
  2537. case AV_CODEC_ID_PCM_MULAW:
  2538. case AV_CODEC_ID_PCM_S8:
  2539. case AV_CODEC_ID_PCM_S8_PLANAR:
  2540. case AV_CODEC_ID_PCM_U8:
  2541. case AV_CODEC_ID_PCM_ZORK:
  2542. case AV_CODEC_ID_SDX2_DPCM:
  2543. return 8;
  2544. case AV_CODEC_ID_PCM_S16BE:
  2545. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2546. case AV_CODEC_ID_PCM_S16LE:
  2547. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2548. case AV_CODEC_ID_PCM_U16BE:
  2549. case AV_CODEC_ID_PCM_U16LE:
  2550. return 16;
  2551. case AV_CODEC_ID_PCM_S24DAUD:
  2552. case AV_CODEC_ID_PCM_S24BE:
  2553. case AV_CODEC_ID_PCM_S24LE:
  2554. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2555. case AV_CODEC_ID_PCM_U24BE:
  2556. case AV_CODEC_ID_PCM_U24LE:
  2557. return 24;
  2558. case AV_CODEC_ID_PCM_S32BE:
  2559. case AV_CODEC_ID_PCM_S32LE:
  2560. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2561. case AV_CODEC_ID_PCM_U32BE:
  2562. case AV_CODEC_ID_PCM_U32LE:
  2563. case AV_CODEC_ID_PCM_F32BE:
  2564. case AV_CODEC_ID_PCM_F32LE:
  2565. return 32;
  2566. case AV_CODEC_ID_PCM_F64BE:
  2567. case AV_CODEC_ID_PCM_F64LE:
  2568. return 64;
  2569. default:
  2570. return 0;
  2571. }
  2572. }
  2573. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2574. {
  2575. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2576. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2577. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2578. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2579. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2580. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2581. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2582. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2583. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2584. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2585. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2586. };
  2587. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2588. return AV_CODEC_ID_NONE;
  2589. if (be < 0 || be > 1)
  2590. be = AV_NE(1, 0);
  2591. return map[fmt][be];
  2592. }
  2593. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2594. {
  2595. switch (codec_id) {
  2596. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2597. return 2;
  2598. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2599. return 3;
  2600. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2601. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2602. case AV_CODEC_ID_ADPCM_IMA_QT:
  2603. case AV_CODEC_ID_ADPCM_SWF:
  2604. case AV_CODEC_ID_ADPCM_MS:
  2605. return 4;
  2606. default:
  2607. return av_get_exact_bits_per_sample(codec_id);
  2608. }
  2609. }
  2610. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2611. {
  2612. int id, sr, ch, ba, tag, bps;
  2613. id = avctx->codec_id;
  2614. sr = avctx->sample_rate;
  2615. ch = avctx->channels;
  2616. ba = avctx->block_align;
  2617. tag = avctx->codec_tag;
  2618. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2619. /* codecs with an exact constant bits per sample */
  2620. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2621. return (frame_bytes * 8LL) / (bps * ch);
  2622. bps = avctx->bits_per_coded_sample;
  2623. /* codecs with a fixed packet duration */
  2624. switch (id) {
  2625. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2626. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2627. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2628. case AV_CODEC_ID_AMR_NB:
  2629. case AV_CODEC_ID_EVRC:
  2630. case AV_CODEC_ID_GSM:
  2631. case AV_CODEC_ID_QCELP:
  2632. case AV_CODEC_ID_RA_288: return 160;
  2633. case AV_CODEC_ID_AMR_WB:
  2634. case AV_CODEC_ID_GSM_MS: return 320;
  2635. case AV_CODEC_ID_MP1: return 384;
  2636. case AV_CODEC_ID_ATRAC1: return 512;
  2637. case AV_CODEC_ID_ATRAC3: return 1024;
  2638. case AV_CODEC_ID_ATRAC3P: return 2048;
  2639. case AV_CODEC_ID_MP2:
  2640. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2641. case AV_CODEC_ID_AC3: return 1536;
  2642. }
  2643. if (sr > 0) {
  2644. /* calc from sample rate */
  2645. if (id == AV_CODEC_ID_TTA)
  2646. return 256 * sr / 245;
  2647. if (ch > 0) {
  2648. /* calc from sample rate and channels */
  2649. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2650. return (480 << (sr / 22050)) / ch;
  2651. }
  2652. }
  2653. if (ba > 0) {
  2654. /* calc from block_align */
  2655. if (id == AV_CODEC_ID_SIPR) {
  2656. switch (ba) {
  2657. case 20: return 160;
  2658. case 19: return 144;
  2659. case 29: return 288;
  2660. case 37: return 480;
  2661. }
  2662. } else if (id == AV_CODEC_ID_ILBC) {
  2663. switch (ba) {
  2664. case 38: return 160;
  2665. case 50: return 240;
  2666. }
  2667. }
  2668. }
  2669. if (frame_bytes > 0) {
  2670. /* calc from frame_bytes only */
  2671. if (id == AV_CODEC_ID_TRUESPEECH)
  2672. return 240 * (frame_bytes / 32);
  2673. if (id == AV_CODEC_ID_NELLYMOSER)
  2674. return 256 * (frame_bytes / 64);
  2675. if (id == AV_CODEC_ID_RA_144)
  2676. return 160 * (frame_bytes / 20);
  2677. if (id == AV_CODEC_ID_G723_1)
  2678. return 240 * (frame_bytes / 24);
  2679. if (bps > 0) {
  2680. /* calc from frame_bytes and bits_per_coded_sample */
  2681. if (id == AV_CODEC_ID_ADPCM_G726)
  2682. return frame_bytes * 8 / bps;
  2683. }
  2684. if (ch > 0) {
  2685. /* calc from frame_bytes and channels */
  2686. switch (id) {
  2687. case AV_CODEC_ID_ADPCM_AFC:
  2688. return frame_bytes / (9 * ch) * 16;
  2689. case AV_CODEC_ID_ADPCM_PSX:
  2690. case AV_CODEC_ID_ADPCM_DTK:
  2691. return frame_bytes / (16 * ch) * 28;
  2692. case AV_CODEC_ID_ADPCM_4XM:
  2693. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2694. return (frame_bytes - 4 * ch) * 2 / ch;
  2695. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2696. return (frame_bytes - 4) * 2 / ch;
  2697. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2698. return (frame_bytes - 8) * 2 / ch;
  2699. case AV_CODEC_ID_ADPCM_THP:
  2700. case AV_CODEC_ID_ADPCM_THP_LE:
  2701. if (avctx->extradata)
  2702. return frame_bytes * 14 / (8 * ch);
  2703. break;
  2704. case AV_CODEC_ID_ADPCM_XA:
  2705. return (frame_bytes / 128) * 224 / ch;
  2706. case AV_CODEC_ID_INTERPLAY_DPCM:
  2707. return (frame_bytes - 6 - ch) / ch;
  2708. case AV_CODEC_ID_ROQ_DPCM:
  2709. return (frame_bytes - 8) / ch;
  2710. case AV_CODEC_ID_XAN_DPCM:
  2711. return (frame_bytes - 2 * ch) / ch;
  2712. case AV_CODEC_ID_MACE3:
  2713. return 3 * frame_bytes / ch;
  2714. case AV_CODEC_ID_MACE6:
  2715. return 6 * frame_bytes / ch;
  2716. case AV_CODEC_ID_PCM_LXF:
  2717. return 2 * (frame_bytes / (5 * ch));
  2718. case AV_CODEC_ID_IAC:
  2719. case AV_CODEC_ID_IMC:
  2720. return 4 * frame_bytes / ch;
  2721. }
  2722. if (tag) {
  2723. /* calc from frame_bytes, channels, and codec_tag */
  2724. if (id == AV_CODEC_ID_SOL_DPCM) {
  2725. if (tag == 3)
  2726. return frame_bytes / ch;
  2727. else
  2728. return frame_bytes * 2 / ch;
  2729. }
  2730. }
  2731. if (ba > 0) {
  2732. /* calc from frame_bytes, channels, and block_align */
  2733. int blocks = frame_bytes / ba;
  2734. switch (avctx->codec_id) {
  2735. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2736. if (bps < 2 || bps > 5)
  2737. return 0;
  2738. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  2739. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2740. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2741. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2742. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2743. case AV_CODEC_ID_ADPCM_IMA_RAD:
  2744. return blocks * ((ba - 4 * ch) * 2 / ch);
  2745. case AV_CODEC_ID_ADPCM_MS:
  2746. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2747. }
  2748. }
  2749. if (bps > 0) {
  2750. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2751. switch (avctx->codec_id) {
  2752. case AV_CODEC_ID_PCM_DVD:
  2753. if(bps<4)
  2754. return 0;
  2755. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2756. case AV_CODEC_ID_PCM_BLURAY:
  2757. if(bps<4)
  2758. return 0;
  2759. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2760. case AV_CODEC_ID_S302M:
  2761. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2762. }
  2763. }
  2764. }
  2765. }
  2766. /* Fall back on using frame_size */
  2767. if (avctx->frame_size > 1 && frame_bytes)
  2768. return avctx->frame_size;
  2769. //For WMA we currently have no other means to calculate duration thus we
  2770. //do it here by assuming CBR, which is true for all known cases.
  2771. if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
  2772. if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
  2773. return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
  2774. }
  2775. return 0;
  2776. }
  2777. #if !HAVE_THREADS
  2778. int ff_thread_init(AVCodecContext *s)
  2779. {
  2780. return -1;
  2781. }
  2782. #endif
  2783. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2784. {
  2785. unsigned int n = 0;
  2786. while (v >= 0xff) {
  2787. *s++ = 0xff;
  2788. v -= 0xff;
  2789. n++;
  2790. }
  2791. *s = v;
  2792. n++;
  2793. return n;
  2794. }
  2795. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2796. {
  2797. int i;
  2798. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2799. return i;
  2800. }
  2801. #if FF_API_MISSING_SAMPLE
  2802. FF_DISABLE_DEPRECATION_WARNINGS
  2803. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2804. {
  2805. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2806. "version to the newest one from Git. If the problem still "
  2807. "occurs, it means that your file has a feature which has not "
  2808. "been implemented.\n", feature);
  2809. if(want_sample)
  2810. av_log_ask_for_sample(avc, NULL);
  2811. }
  2812. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2813. {
  2814. va_list argument_list;
  2815. va_start(argument_list, msg);
  2816. if (msg)
  2817. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2818. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2819. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  2820. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  2821. va_end(argument_list);
  2822. }
  2823. FF_ENABLE_DEPRECATION_WARNINGS
  2824. #endif /* FF_API_MISSING_SAMPLE */
  2825. static AVHWAccel *first_hwaccel = NULL;
  2826. static AVHWAccel **last_hwaccel = &first_hwaccel;
  2827. void av_register_hwaccel(AVHWAccel *hwaccel)
  2828. {
  2829. AVHWAccel **p = last_hwaccel;
  2830. hwaccel->next = NULL;
  2831. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  2832. p = &(*p)->next;
  2833. last_hwaccel = &hwaccel->next;
  2834. }
  2835. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  2836. {
  2837. return hwaccel ? hwaccel->next : first_hwaccel;
  2838. }
  2839. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2840. {
  2841. if (lockmgr_cb) {
  2842. // There is no good way to rollback a failure to destroy the
  2843. // mutex, so we ignore failures.
  2844. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  2845. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  2846. lockmgr_cb = NULL;
  2847. codec_mutex = NULL;
  2848. avformat_mutex = NULL;
  2849. }
  2850. if (cb) {
  2851. void *new_codec_mutex = NULL;
  2852. void *new_avformat_mutex = NULL;
  2853. int err;
  2854. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  2855. return err > 0 ? AVERROR_UNKNOWN : err;
  2856. }
  2857. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  2858. // Ignore failures to destroy the newly created mutex.
  2859. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  2860. return err > 0 ? AVERROR_UNKNOWN : err;
  2861. }
  2862. lockmgr_cb = cb;
  2863. codec_mutex = new_codec_mutex;
  2864. avformat_mutex = new_avformat_mutex;
  2865. }
  2866. return 0;
  2867. }
  2868. int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
  2869. {
  2870. if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
  2871. return 0;
  2872. if (lockmgr_cb) {
  2873. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2874. return -1;
  2875. }
  2876. if (avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, 1) != 1) {
  2877. av_log(log_ctx, AV_LOG_ERROR,
  2878. "Insufficient thread locking. At least %d threads are "
  2879. "calling avcodec_open2() at the same time right now.\n",
  2880. entangled_thread_counter);
  2881. if (!lockmgr_cb)
  2882. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  2883. ff_avcodec_locked = 1;
  2884. ff_unlock_avcodec(codec);
  2885. return AVERROR(EINVAL);
  2886. }
  2887. av_assert0(!ff_avcodec_locked);
  2888. ff_avcodec_locked = 1;
  2889. return 0;
  2890. }
  2891. int ff_unlock_avcodec(const AVCodec *codec)
  2892. {
  2893. if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
  2894. return 0;
  2895. av_assert0(ff_avcodec_locked);
  2896. ff_avcodec_locked = 0;
  2897. avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, -1);
  2898. if (lockmgr_cb) {
  2899. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2900. return -1;
  2901. }
  2902. return 0;
  2903. }
  2904. int avpriv_lock_avformat(void)
  2905. {
  2906. if (lockmgr_cb) {
  2907. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2908. return -1;
  2909. }
  2910. return 0;
  2911. }
  2912. int avpriv_unlock_avformat(void)
  2913. {
  2914. if (lockmgr_cb) {
  2915. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2916. return -1;
  2917. }
  2918. return 0;
  2919. }
  2920. unsigned int avpriv_toupper4(unsigned int x)
  2921. {
  2922. return av_toupper(x & 0xFF) +
  2923. (av_toupper((x >> 8) & 0xFF) << 8) +
  2924. (av_toupper((x >> 16) & 0xFF) << 16) +
  2925. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  2926. }
  2927. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2928. {
  2929. int ret;
  2930. dst->owner = src->owner;
  2931. ret = av_frame_ref(dst->f, src->f);
  2932. if (ret < 0)
  2933. return ret;
  2934. av_assert0(!dst->progress);
  2935. if (src->progress &&
  2936. !(dst->progress = av_buffer_ref(src->progress))) {
  2937. ff_thread_release_buffer(dst->owner, dst);
  2938. return AVERROR(ENOMEM);
  2939. }
  2940. return 0;
  2941. }
  2942. #if !HAVE_THREADS
  2943. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  2944. {
  2945. return ff_get_format(avctx, fmt);
  2946. }
  2947. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2948. {
  2949. f->owner = avctx;
  2950. return ff_get_buffer(avctx, f->f, flags);
  2951. }
  2952. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2953. {
  2954. if (f->f)
  2955. av_frame_unref(f->f);
  2956. }
  2957. void ff_thread_finish_setup(AVCodecContext *avctx)
  2958. {
  2959. }
  2960. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2961. {
  2962. }
  2963. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2964. {
  2965. }
  2966. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2967. {
  2968. return 1;
  2969. }
  2970. int ff_alloc_entries(AVCodecContext *avctx, int count)
  2971. {
  2972. return 0;
  2973. }
  2974. void ff_reset_entries(AVCodecContext *avctx)
  2975. {
  2976. }
  2977. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  2978. {
  2979. }
  2980. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  2981. {
  2982. }
  2983. #endif
  2984. int avcodec_is_open(AVCodecContext *s)
  2985. {
  2986. return !!s->internal;
  2987. }
  2988. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2989. {
  2990. int ret;
  2991. char *str;
  2992. ret = av_bprint_finalize(buf, &str);
  2993. if (ret < 0)
  2994. return ret;
  2995. if (!av_bprint_is_complete(buf)) {
  2996. av_free(str);
  2997. return AVERROR(ENOMEM);
  2998. }
  2999. avctx->extradata = str;
  3000. /* Note: the string is NUL terminated (so extradata can be read as a
  3001. * string), but the ending character is not accounted in the size (in
  3002. * binary formats you are likely not supposed to mux that character). When
  3003. * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
  3004. * zeros. */
  3005. avctx->extradata_size = buf->len;
  3006. return 0;
  3007. }
  3008. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3009. const uint8_t *end,
  3010. uint32_t *av_restrict state)
  3011. {
  3012. int i;
  3013. av_assert0(p <= end);
  3014. if (p >= end)
  3015. return end;
  3016. for (i = 0; i < 3; i++) {
  3017. uint32_t tmp = *state << 8;
  3018. *state = tmp + *(p++);
  3019. if (tmp == 0x100 || p == end)
  3020. return p;
  3021. }
  3022. while (p < end) {
  3023. if (p[-1] > 1 ) p += 3;
  3024. else if (p[-2] ) p += 2;
  3025. else if (p[-3]|(p[-1]-1)) p++;
  3026. else {
  3027. p++;
  3028. break;
  3029. }
  3030. }
  3031. p = FFMIN(p, end) - 4;
  3032. *state = AV_RB32(p);
  3033. return p + 4;
  3034. }