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.

3262 lines
106KB

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