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.

2662 lines
84KB

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