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.

2687 lines
86KB

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