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.

2593 lines
83KB

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