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.

2172 lines
65KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "avcodec.h"
  36. #include "dsputil.h"
  37. #include "libavutil/opt.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include "bytestream.h"
  42. #include <stdlib.h>
  43. #include <stdarg.h>
  44. #include <limits.h>
  45. #include <float.h>
  46. static int volatile entangled_thread_counter = 0;
  47. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  48. static void *codec_mutex;
  49. static void *avformat_mutex;
  50. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  51. {
  52. if (min_size < *size)
  53. return ptr;
  54. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  55. ptr = av_realloc(ptr, min_size);
  56. /* we could set this to the unmodified min_size but this is safer
  57. * if the user lost the ptr and uses NULL now
  58. */
  59. if (!ptr)
  60. min_size = 0;
  61. *size = min_size;
  62. return ptr;
  63. }
  64. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  65. {
  66. void **p = ptr;
  67. if (min_size < *size)
  68. return;
  69. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  70. av_free(*p);
  71. *p = av_malloc(min_size);
  72. if (!*p)
  73. min_size = 0;
  74. *size = min_size;
  75. }
  76. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  77. {
  78. void **p = ptr;
  79. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  80. av_freep(p);
  81. *size = 0;
  82. return;
  83. }
  84. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  85. if (*size)
  86. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  87. }
  88. /* encoder management */
  89. static AVCodec *first_avcodec = NULL;
  90. AVCodec *av_codec_next(const AVCodec *c)
  91. {
  92. if (c)
  93. return c->next;
  94. else
  95. return first_avcodec;
  96. }
  97. static void avcodec_init(void)
  98. {
  99. static int initialized = 0;
  100. if (initialized != 0)
  101. return;
  102. initialized = 1;
  103. ff_dsputil_static_init();
  104. }
  105. int av_codec_is_encoder(const AVCodec *codec)
  106. {
  107. return codec && (codec->encode_sub || codec->encode2);
  108. }
  109. int av_codec_is_decoder(const AVCodec *codec)
  110. {
  111. return codec && codec->decode;
  112. }
  113. void avcodec_register(AVCodec *codec)
  114. {
  115. AVCodec **p;
  116. avcodec_init();
  117. p = &first_avcodec;
  118. while (*p != NULL)
  119. p = &(*p)->next;
  120. *p = codec;
  121. codec->next = NULL;
  122. if (codec->init_static_data)
  123. codec->init_static_data(codec);
  124. }
  125. unsigned avcodec_get_edge_width(void)
  126. {
  127. return EDGE_WIDTH;
  128. }
  129. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  130. {
  131. s->coded_width = width;
  132. s->coded_height = height;
  133. s->width = width;
  134. s->height = height;
  135. }
  136. #define INTERNAL_BUFFER_SIZE (32 + 1)
  137. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  138. int linesize_align[AV_NUM_DATA_POINTERS])
  139. {
  140. int i;
  141. int w_align = 1;
  142. int h_align = 1;
  143. switch (s->pix_fmt) {
  144. case AV_PIX_FMT_YUV420P:
  145. case AV_PIX_FMT_YUYV422:
  146. case AV_PIX_FMT_UYVY422:
  147. case AV_PIX_FMT_YUV422P:
  148. case AV_PIX_FMT_YUV440P:
  149. case AV_PIX_FMT_YUV444P:
  150. case AV_PIX_FMT_GBRP:
  151. case AV_PIX_FMT_GRAY8:
  152. case AV_PIX_FMT_GRAY16BE:
  153. case AV_PIX_FMT_GRAY16LE:
  154. case AV_PIX_FMT_YUVJ420P:
  155. case AV_PIX_FMT_YUVJ422P:
  156. case AV_PIX_FMT_YUVJ440P:
  157. case AV_PIX_FMT_YUVJ444P:
  158. case AV_PIX_FMT_YUVA420P:
  159. case AV_PIX_FMT_YUV420P9LE:
  160. case AV_PIX_FMT_YUV420P9BE:
  161. case AV_PIX_FMT_YUV420P10LE:
  162. case AV_PIX_FMT_YUV420P10BE:
  163. case AV_PIX_FMT_YUV422P9LE:
  164. case AV_PIX_FMT_YUV422P9BE:
  165. case AV_PIX_FMT_YUV422P10LE:
  166. case AV_PIX_FMT_YUV422P10BE:
  167. case AV_PIX_FMT_YUV444P9LE:
  168. case AV_PIX_FMT_YUV444P9BE:
  169. case AV_PIX_FMT_YUV444P10LE:
  170. case AV_PIX_FMT_YUV444P10BE:
  171. case AV_PIX_FMT_GBRP9LE:
  172. case AV_PIX_FMT_GBRP9BE:
  173. case AV_PIX_FMT_GBRP10LE:
  174. case AV_PIX_FMT_GBRP10BE:
  175. w_align = 16; //FIXME assume 16 pixel per macroblock
  176. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  177. break;
  178. case AV_PIX_FMT_YUV411P:
  179. case AV_PIX_FMT_UYYVYY411:
  180. w_align = 32;
  181. h_align = 8;
  182. break;
  183. case AV_PIX_FMT_YUV410P:
  184. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  185. w_align = 64;
  186. h_align = 64;
  187. }
  188. case AV_PIX_FMT_RGB555:
  189. if (s->codec_id == AV_CODEC_ID_RPZA) {
  190. w_align = 4;
  191. h_align = 4;
  192. }
  193. case AV_PIX_FMT_PAL8:
  194. case AV_PIX_FMT_BGR8:
  195. case AV_PIX_FMT_RGB8:
  196. if (s->codec_id == AV_CODEC_ID_SMC) {
  197. w_align = 4;
  198. h_align = 4;
  199. }
  200. break;
  201. case AV_PIX_FMT_BGR24:
  202. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  203. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  204. w_align = 4;
  205. h_align = 4;
  206. }
  207. break;
  208. default:
  209. w_align = 1;
  210. h_align = 1;
  211. break;
  212. }
  213. *width = FFALIGN(*width, w_align);
  214. *height = FFALIGN(*height, h_align);
  215. if (s->codec_id == AV_CODEC_ID_H264)
  216. // some of the optimized chroma MC reads one line too much
  217. *height += 2;
  218. for (i = 0; i < 4; i++)
  219. linesize_align[i] = STRIDE_ALIGN;
  220. }
  221. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  222. {
  223. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  224. int linesize_align[AV_NUM_DATA_POINTERS];
  225. int align;
  226. avcodec_align_dimensions2(s, width, height, linesize_align);
  227. align = FFMAX(linesize_align[0], linesize_align[3]);
  228. linesize_align[1] <<= chroma_shift;
  229. linesize_align[2] <<= chroma_shift;
  230. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  231. *width = FFALIGN(*width, align);
  232. }
  233. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  234. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  235. int buf_size, int align)
  236. {
  237. int ch, planar, needed_size, ret = 0;
  238. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  239. frame->nb_samples, sample_fmt,
  240. align);
  241. if (buf_size < needed_size)
  242. return AVERROR(EINVAL);
  243. planar = av_sample_fmt_is_planar(sample_fmt);
  244. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  245. if (!(frame->extended_data = av_mallocz(nb_channels *
  246. sizeof(*frame->extended_data))))
  247. return AVERROR(ENOMEM);
  248. } else {
  249. frame->extended_data = frame->data;
  250. }
  251. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  252. buf, nb_channels, frame->nb_samples,
  253. sample_fmt, align)) < 0) {
  254. if (frame->extended_data != frame->data)
  255. av_free(frame->extended_data);
  256. return ret;
  257. }
  258. if (frame->extended_data != frame->data) {
  259. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  260. frame->data[ch] = frame->extended_data[ch];
  261. }
  262. return ret;
  263. }
  264. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  265. {
  266. AVCodecInternal *avci = avctx->internal;
  267. InternalBuffer *buf;
  268. int buf_size, ret;
  269. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  270. frame->nb_samples, avctx->sample_fmt,
  271. 0);
  272. if (buf_size < 0)
  273. return AVERROR(EINVAL);
  274. /* allocate InternalBuffer if needed */
  275. if (!avci->buffer) {
  276. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  277. if (!avci->buffer)
  278. return AVERROR(ENOMEM);
  279. }
  280. buf = avci->buffer;
  281. /* if there is a previously-used internal buffer, check its size and
  282. * channel count to see if we can reuse it */
  283. if (buf->extended_data) {
  284. /* if current buffer is too small, free it */
  285. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  286. av_free(buf->extended_data[0]);
  287. if (buf->extended_data != buf->data)
  288. av_free(buf->extended_data);
  289. buf->extended_data = NULL;
  290. buf->data[0] = NULL;
  291. }
  292. /* if number of channels has changed, reset and/or free extended data
  293. * pointers but leave data buffer in buf->data[0] for reuse */
  294. if (buf->nb_channels != avctx->channels) {
  295. if (buf->extended_data != buf->data)
  296. av_free(buf->extended_data);
  297. buf->extended_data = NULL;
  298. }
  299. }
  300. /* if there is no previous buffer or the previous buffer cannot be used
  301. * as-is, allocate a new buffer and/or rearrange the channel pointers */
  302. if (!buf->extended_data) {
  303. if (!buf->data[0]) {
  304. if (!(buf->data[0] = av_mallocz(buf_size)))
  305. return AVERROR(ENOMEM);
  306. buf->audio_data_size = buf_size;
  307. }
  308. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  309. avctx->sample_fmt, buf->data[0],
  310. buf->audio_data_size, 0)))
  311. return ret;
  312. if (frame->extended_data == frame->data)
  313. buf->extended_data = buf->data;
  314. else
  315. buf->extended_data = frame->extended_data;
  316. memcpy(buf->data, frame->data, sizeof(frame->data));
  317. buf->linesize[0] = frame->linesize[0];
  318. buf->nb_channels = avctx->channels;
  319. } else {
  320. /* copy InternalBuffer info to the AVFrame */
  321. frame->extended_data = buf->extended_data;
  322. frame->linesize[0] = buf->linesize[0];
  323. memcpy(frame->data, buf->data, sizeof(frame->data));
  324. }
  325. frame->type = FF_BUFFER_TYPE_INTERNAL;
  326. if (avctx->pkt)
  327. frame->pkt_pts = avctx->pkt->pts;
  328. else
  329. frame->pkt_pts = AV_NOPTS_VALUE;
  330. frame->reordered_opaque = avctx->reordered_opaque;
  331. frame->sample_rate = avctx->sample_rate;
  332. frame->format = avctx->sample_fmt;
  333. frame->channel_layout = avctx->channel_layout;
  334. if (avctx->debug & FF_DEBUG_BUFFERS)
  335. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  336. "internal audio buffer used\n", frame);
  337. return 0;
  338. }
  339. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  340. {
  341. int i;
  342. int w = s->width;
  343. int h = s->height;
  344. InternalBuffer *buf;
  345. AVCodecInternal *avci = s->internal;
  346. if (pic->data[0] != NULL) {
  347. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  348. return -1;
  349. }
  350. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  351. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  352. return -1;
  353. }
  354. if (av_image_check_size(w, h, 0, s))
  355. return -1;
  356. if (!avci->buffer) {
  357. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  358. sizeof(InternalBuffer));
  359. }
  360. buf = &avci->buffer[avci->buffer_count];
  361. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  362. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  363. av_freep(&buf->base[i]);
  364. buf->data[i] = NULL;
  365. }
  366. }
  367. if (!buf->base[0]) {
  368. int h_chroma_shift, v_chroma_shift;
  369. int size[4] = { 0 };
  370. int tmpsize;
  371. int unaligned;
  372. AVPicture picture;
  373. int stride_align[AV_NUM_DATA_POINTERS];
  374. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1 + 1;
  375. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  376. avcodec_align_dimensions2(s, &w, &h, stride_align);
  377. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  378. w += EDGE_WIDTH * 2;
  379. h += EDGE_WIDTH * 2;
  380. }
  381. do {
  382. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  383. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  384. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  385. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  386. w += w & ~(w - 1);
  387. unaligned = 0;
  388. for (i = 0; i < 4; i++)
  389. unaligned |= picture.linesize[i] % stride_align[i];
  390. } while (unaligned);
  391. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  392. if (tmpsize < 0)
  393. return -1;
  394. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  395. size[i] = picture.data[i + 1] - picture.data[i];
  396. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  397. memset(buf->base, 0, sizeof(buf->base));
  398. memset(buf->data, 0, sizeof(buf->data));
  399. for (i = 0; i < 4 && size[i]; i++) {
  400. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  401. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  402. buf->linesize[i] = picture.linesize[i];
  403. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  404. if (buf->base[i] == NULL)
  405. return -1;
  406. memset(buf->base[i], 128, size[i]);
  407. // no edge if EDGE EMU or not planar YUV
  408. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  409. buf->data[i] = buf->base[i];
  410. else
  411. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  412. }
  413. for (; i < AV_NUM_DATA_POINTERS; i++) {
  414. buf->base[i] = buf->data[i] = NULL;
  415. buf->linesize[i] = 0;
  416. }
  417. if (size[1] && !size[2])
  418. ff_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  419. buf->width = s->width;
  420. buf->height = s->height;
  421. buf->pix_fmt = s->pix_fmt;
  422. }
  423. pic->type = FF_BUFFER_TYPE_INTERNAL;
  424. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  425. pic->base[i] = buf->base[i];
  426. pic->data[i] = buf->data[i];
  427. pic->linesize[i] = buf->linesize[i];
  428. }
  429. pic->extended_data = pic->data;
  430. avci->buffer_count++;
  431. pic->width = buf->width;
  432. pic->height = buf->height;
  433. pic->format = buf->pix_fmt;
  434. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  435. if (s->pkt)
  436. pic->pkt_pts = s->pkt->pts;
  437. else
  438. pic->pkt_pts = AV_NOPTS_VALUE;
  439. pic->reordered_opaque = s->reordered_opaque;
  440. if (s->debug & FF_DEBUG_BUFFERS)
  441. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  442. "buffers used\n", pic, avci->buffer_count);
  443. return 0;
  444. }
  445. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  446. {
  447. switch (avctx->codec_type) {
  448. case AVMEDIA_TYPE_VIDEO:
  449. return video_get_buffer(avctx, frame);
  450. case AVMEDIA_TYPE_AUDIO:
  451. return audio_get_buffer(avctx, frame);
  452. default:
  453. return -1;
  454. }
  455. }
  456. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  457. {
  458. int i;
  459. InternalBuffer *buf, *last;
  460. AVCodecInternal *avci = s->internal;
  461. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  462. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  463. assert(avci->buffer_count);
  464. if (avci->buffer) {
  465. buf = NULL; /* avoids warning */
  466. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  467. buf = &avci->buffer[i];
  468. if (buf->data[0] == pic->data[0])
  469. break;
  470. }
  471. assert(i < avci->buffer_count);
  472. avci->buffer_count--;
  473. last = &avci->buffer[avci->buffer_count];
  474. if (buf != last)
  475. FFSWAP(InternalBuffer, *buf, *last);
  476. }
  477. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  478. pic->data[i] = NULL;
  479. // pic->base[i]=NULL;
  480. if (s->debug & FF_DEBUG_BUFFERS)
  481. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  482. "buffers used\n", pic, avci->buffer_count);
  483. }
  484. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  485. {
  486. AVFrame temp_pic;
  487. int i;
  488. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  489. /* If no picture return a new buffer */
  490. if (pic->data[0] == NULL) {
  491. /* We will copy from buffer, so must be readable */
  492. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  493. return s->get_buffer(s, pic);
  494. }
  495. assert(s->pix_fmt == pic->format);
  496. /* If internal buffer type return the same buffer */
  497. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  498. if (s->pkt)
  499. pic->pkt_pts = s->pkt->pts;
  500. else
  501. pic->pkt_pts = AV_NOPTS_VALUE;
  502. pic->reordered_opaque = s->reordered_opaque;
  503. return 0;
  504. }
  505. /*
  506. * Not internal type and reget_buffer not overridden, emulate cr buffer
  507. */
  508. temp_pic = *pic;
  509. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  510. pic->data[i] = pic->base[i] = NULL;
  511. pic->opaque = NULL;
  512. /* Allocate new frame */
  513. if (s->get_buffer(s, pic))
  514. return -1;
  515. /* Copy image data from old buffer to new buffer */
  516. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  517. s->height);
  518. s->release_buffer(s, &temp_pic); // Release old frame
  519. return 0;
  520. }
  521. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  522. {
  523. int i;
  524. for (i = 0; i < count; i++) {
  525. int r = func(c, (char *)arg + i * size);
  526. if (ret)
  527. ret[i] = r;
  528. }
  529. return 0;
  530. }
  531. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  532. {
  533. int i;
  534. for (i = 0; i < count; i++) {
  535. int r = func(c, arg, i, 0);
  536. if (ret)
  537. ret[i] = r;
  538. }
  539. return 0;
  540. }
  541. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  542. {
  543. while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  544. ++fmt;
  545. return fmt[0];
  546. }
  547. void avcodec_get_frame_defaults(AVFrame *frame)
  548. {
  549. if (frame->extended_data != frame->data)
  550. av_freep(&frame->extended_data);
  551. memset(frame, 0, sizeof(AVFrame));
  552. frame->pts = AV_NOPTS_VALUE;
  553. frame->key_frame = 1;
  554. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  555. frame->format = -1; /* unknown */
  556. frame->extended_data = frame->data;
  557. }
  558. AVFrame *avcodec_alloc_frame(void)
  559. {
  560. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  561. if (frame == NULL)
  562. return NULL;
  563. avcodec_get_frame_defaults(frame);
  564. return frame;
  565. }
  566. void avcodec_free_frame(AVFrame **frame)
  567. {
  568. AVFrame *f;
  569. if (!frame || !*frame)
  570. return;
  571. f = *frame;
  572. if (f->extended_data != f->data)
  573. av_freep(&f->extended_data);
  574. av_freep(frame);
  575. }
  576. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  577. {
  578. int ret = 0;
  579. AVDictionary *tmp = NULL;
  580. if (avcodec_is_open(avctx))
  581. return 0;
  582. if ((!codec && !avctx->codec)) {
  583. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  584. return AVERROR(EINVAL);
  585. }
  586. if ((codec && avctx->codec && codec != avctx->codec)) {
  587. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  588. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  589. return AVERROR(EINVAL);
  590. }
  591. if (!codec)
  592. codec = avctx->codec;
  593. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  594. return AVERROR(EINVAL);
  595. if (options)
  596. av_dict_copy(&tmp, *options, 0);
  597. /* If there is a user-supplied mutex locking routine, call it. */
  598. if (ff_lockmgr_cb) {
  599. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  600. return -1;
  601. }
  602. entangled_thread_counter++;
  603. if (entangled_thread_counter != 1) {
  604. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  605. ret = -1;
  606. goto end;
  607. }
  608. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  609. if (!avctx->internal) {
  610. ret = AVERROR(ENOMEM);
  611. goto end;
  612. }
  613. if (codec->priv_data_size > 0) {
  614. if (!avctx->priv_data) {
  615. avctx->priv_data = av_mallocz(codec->priv_data_size);
  616. if (!avctx->priv_data) {
  617. ret = AVERROR(ENOMEM);
  618. goto end;
  619. }
  620. if (codec->priv_class) {
  621. *(const AVClass **)avctx->priv_data = codec->priv_class;
  622. av_opt_set_defaults(avctx->priv_data);
  623. }
  624. }
  625. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  626. goto free_and_end;
  627. } else {
  628. avctx->priv_data = NULL;
  629. }
  630. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  631. goto free_and_end;
  632. if (avctx->coded_width && avctx->coded_height)
  633. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  634. else if (avctx->width && avctx->height)
  635. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  636. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  637. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  638. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  639. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  640. avcodec_set_dimensions(avctx, 0, 0);
  641. }
  642. /* if the decoder init function was already called previously,
  643. * free the already allocated subtitle_header before overwriting it */
  644. if (av_codec_is_decoder(codec))
  645. av_freep(&avctx->subtitle_header);
  646. #define SANE_NB_CHANNELS 128U
  647. if (avctx->channels > SANE_NB_CHANNELS) {
  648. ret = AVERROR(EINVAL);
  649. goto free_and_end;
  650. }
  651. avctx->codec = codec;
  652. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  653. avctx->codec_id == AV_CODEC_ID_NONE) {
  654. avctx->codec_type = codec->type;
  655. avctx->codec_id = codec->id;
  656. }
  657. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  658. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  659. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  660. ret = AVERROR(EINVAL);
  661. goto free_and_end;
  662. }
  663. avctx->frame_number = 0;
  664. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  665. (!avctx->time_base.num || !avctx->time_base.den)) {
  666. avctx->time_base.num = 1;
  667. avctx->time_base.den = avctx->sample_rate;
  668. }
  669. if (HAVE_THREADS && !avctx->thread_opaque) {
  670. ret = ff_thread_init(avctx);
  671. if (ret < 0) {
  672. goto free_and_end;
  673. }
  674. }
  675. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  676. avctx->thread_count = 1;
  677. if (av_codec_is_encoder(avctx->codec)) {
  678. int i;
  679. if (avctx->codec->sample_fmts) {
  680. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  681. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  682. break;
  683. if (avctx->channels == 1 &&
  684. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  685. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  686. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  687. break;
  688. }
  689. }
  690. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  691. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  692. ret = AVERROR(EINVAL);
  693. goto free_and_end;
  694. }
  695. }
  696. if (avctx->codec->pix_fmts) {
  697. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  698. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  699. break;
  700. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  701. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  702. ret = AVERROR(EINVAL);
  703. goto free_and_end;
  704. }
  705. }
  706. if (avctx->codec->supported_samplerates) {
  707. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  708. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  709. break;
  710. if (avctx->codec->supported_samplerates[i] == 0) {
  711. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  712. ret = AVERROR(EINVAL);
  713. goto free_and_end;
  714. }
  715. }
  716. if (avctx->codec->channel_layouts) {
  717. if (!avctx->channel_layout) {
  718. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  719. } else {
  720. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  721. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  722. break;
  723. if (avctx->codec->channel_layouts[i] == 0) {
  724. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  725. ret = AVERROR(EINVAL);
  726. goto free_and_end;
  727. }
  728. }
  729. }
  730. if (avctx->channel_layout && avctx->channels) {
  731. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  732. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  733. ret = AVERROR(EINVAL);
  734. goto free_and_end;
  735. }
  736. } else if (avctx->channel_layout) {
  737. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  738. }
  739. }
  740. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  741. ret = avctx->codec->init(avctx);
  742. if (ret < 0) {
  743. goto free_and_end;
  744. }
  745. }
  746. if (av_codec_is_decoder(avctx->codec)) {
  747. /* validate channel layout from the decoder */
  748. if (avctx->channel_layout) {
  749. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  750. if (!avctx->channels)
  751. avctx->channels = channels;
  752. else if (channels != avctx->channels) {
  753. av_log(avctx, AV_LOG_WARNING,
  754. "channel layout does not match number of channels\n");
  755. avctx->channel_layout = 0;
  756. }
  757. }
  758. }
  759. end:
  760. entangled_thread_counter--;
  761. /* Release any user-supplied mutex. */
  762. if (ff_lockmgr_cb) {
  763. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  764. }
  765. if (options) {
  766. av_dict_free(options);
  767. *options = tmp;
  768. }
  769. return ret;
  770. free_and_end:
  771. av_dict_free(&tmp);
  772. av_freep(&avctx->priv_data);
  773. av_freep(&avctx->internal);
  774. avctx->codec = NULL;
  775. goto end;
  776. }
  777. int ff_alloc_packet(AVPacket *avpkt, int size)
  778. {
  779. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  780. return AVERROR(EINVAL);
  781. if (avpkt->data) {
  782. void *destruct = avpkt->destruct;
  783. if (avpkt->size < size)
  784. return AVERROR(EINVAL);
  785. av_init_packet(avpkt);
  786. avpkt->destruct = destruct;
  787. avpkt->size = size;
  788. return 0;
  789. } else {
  790. return av_new_packet(avpkt, size);
  791. }
  792. }
  793. /**
  794. * Pad last frame with silence.
  795. */
  796. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  797. {
  798. AVFrame *frame = NULL;
  799. uint8_t *buf = NULL;
  800. int ret;
  801. if (!(frame = avcodec_alloc_frame()))
  802. return AVERROR(ENOMEM);
  803. *frame = *src;
  804. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  805. s->frame_size, s->sample_fmt, 0)) < 0)
  806. goto fail;
  807. if (!(buf = av_malloc(ret))) {
  808. ret = AVERROR(ENOMEM);
  809. goto fail;
  810. }
  811. frame->nb_samples = s->frame_size;
  812. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  813. buf, ret, 0)) < 0)
  814. goto fail;
  815. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  816. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  817. goto fail;
  818. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  819. frame->nb_samples - src->nb_samples,
  820. s->channels, s->sample_fmt)) < 0)
  821. goto fail;
  822. *dst = frame;
  823. return 0;
  824. fail:
  825. if (frame->extended_data != frame->data)
  826. av_freep(&frame->extended_data);
  827. av_freep(&buf);
  828. av_freep(&frame);
  829. return ret;
  830. }
  831. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  832. AVPacket *avpkt,
  833. const AVFrame *frame,
  834. int *got_packet_ptr)
  835. {
  836. AVFrame tmp;
  837. AVFrame *padded_frame = NULL;
  838. int ret;
  839. int user_packet = !!avpkt->data;
  840. *got_packet_ptr = 0;
  841. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  842. av_free_packet(avpkt);
  843. av_init_packet(avpkt);
  844. return 0;
  845. }
  846. /* ensure that extended_data is properly set */
  847. if (frame && !frame->extended_data) {
  848. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  849. avctx->channels > AV_NUM_DATA_POINTERS) {
  850. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  851. "with more than %d channels, but extended_data is not set.\n",
  852. AV_NUM_DATA_POINTERS);
  853. return AVERROR(EINVAL);
  854. }
  855. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  856. tmp = *frame;
  857. tmp.extended_data = tmp.data;
  858. frame = &tmp;
  859. }
  860. /* check for valid frame size */
  861. if (frame) {
  862. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  863. if (frame->nb_samples > avctx->frame_size)
  864. return AVERROR(EINVAL);
  865. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  866. if (frame->nb_samples < avctx->frame_size &&
  867. !avctx->internal->last_audio_frame) {
  868. ret = pad_last_frame(avctx, &padded_frame, frame);
  869. if (ret < 0)
  870. return ret;
  871. frame = padded_frame;
  872. avctx->internal->last_audio_frame = 1;
  873. }
  874. if (frame->nb_samples != avctx->frame_size) {
  875. ret = AVERROR(EINVAL);
  876. goto end;
  877. }
  878. }
  879. }
  880. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  881. if (!ret) {
  882. if (*got_packet_ptr) {
  883. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  884. if (avpkt->pts == AV_NOPTS_VALUE)
  885. avpkt->pts = frame->pts;
  886. if (!avpkt->duration)
  887. avpkt->duration = ff_samples_to_time_base(avctx,
  888. frame->nb_samples);
  889. }
  890. avpkt->dts = avpkt->pts;
  891. } else {
  892. avpkt->size = 0;
  893. }
  894. if (!user_packet && avpkt->size) {
  895. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  896. if (new_data)
  897. avpkt->data = new_data;
  898. }
  899. avctx->frame_number++;
  900. }
  901. if (ret < 0 || !*got_packet_ptr) {
  902. av_free_packet(avpkt);
  903. av_init_packet(avpkt);
  904. goto end;
  905. }
  906. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  907. * this needs to be moved to the encoders, but for now we can do it
  908. * here to simplify things */
  909. avpkt->flags |= AV_PKT_FLAG_KEY;
  910. end:
  911. if (padded_frame) {
  912. av_freep(&padded_frame->data[0]);
  913. if (padded_frame->extended_data != padded_frame->data)
  914. av_freep(&padded_frame->extended_data);
  915. av_freep(&padded_frame);
  916. }
  917. return ret;
  918. }
  919. #if FF_API_OLD_DECODE_AUDIO
  920. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  921. uint8_t *buf, int buf_size,
  922. const short *samples)
  923. {
  924. AVPacket pkt;
  925. AVFrame frame0;
  926. AVFrame *frame;
  927. int ret, samples_size, got_packet;
  928. av_init_packet(&pkt);
  929. pkt.data = buf;
  930. pkt.size = buf_size;
  931. if (samples) {
  932. frame = &frame0;
  933. avcodec_get_frame_defaults(frame);
  934. if (avctx->frame_size) {
  935. frame->nb_samples = avctx->frame_size;
  936. } else {
  937. /* if frame_size is not set, the number of samples must be
  938. * calculated from the buffer size */
  939. int64_t nb_samples;
  940. if (!av_get_bits_per_sample(avctx->codec_id)) {
  941. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  942. "support this codec\n");
  943. return AVERROR(EINVAL);
  944. }
  945. nb_samples = (int64_t)buf_size * 8 /
  946. (av_get_bits_per_sample(avctx->codec_id) *
  947. avctx->channels);
  948. if (nb_samples >= INT_MAX)
  949. return AVERROR(EINVAL);
  950. frame->nb_samples = nb_samples;
  951. }
  952. /* it is assumed that the samples buffer is large enough based on the
  953. * relevant parameters */
  954. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  955. frame->nb_samples,
  956. avctx->sample_fmt, 1);
  957. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  958. avctx->sample_fmt,
  959. (const uint8_t *)samples,
  960. samples_size, 1)))
  961. return ret;
  962. /* fabricate frame pts from sample count.
  963. * this is needed because the avcodec_encode_audio() API does not have
  964. * a way for the user to provide pts */
  965. frame->pts = ff_samples_to_time_base(avctx,
  966. avctx->internal->sample_count);
  967. avctx->internal->sample_count += frame->nb_samples;
  968. } else {
  969. frame = NULL;
  970. }
  971. got_packet = 0;
  972. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  973. if (!ret && got_packet && avctx->coded_frame) {
  974. avctx->coded_frame->pts = pkt.pts;
  975. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  976. }
  977. /* free any side data since we cannot return it */
  978. if (pkt.side_data_elems > 0) {
  979. int i;
  980. for (i = 0; i < pkt.side_data_elems; i++)
  981. av_free(pkt.side_data[i].data);
  982. av_freep(&pkt.side_data);
  983. pkt.side_data_elems = 0;
  984. }
  985. if (frame && frame->extended_data != frame->data)
  986. av_free(frame->extended_data);
  987. return ret ? ret : pkt.size;
  988. }
  989. #endif
  990. #if FF_API_OLD_ENCODE_VIDEO
  991. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  992. const AVFrame *pict)
  993. {
  994. AVPacket pkt;
  995. int ret, got_packet = 0;
  996. if (buf_size < FF_MIN_BUFFER_SIZE) {
  997. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  998. return -1;
  999. }
  1000. av_init_packet(&pkt);
  1001. pkt.data = buf;
  1002. pkt.size = buf_size;
  1003. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1004. if (!ret && got_packet && avctx->coded_frame) {
  1005. avctx->coded_frame->pts = pkt.pts;
  1006. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1007. }
  1008. /* free any side data since we cannot return it */
  1009. if (pkt.side_data_elems > 0) {
  1010. int i;
  1011. for (i = 0; i < pkt.side_data_elems; i++)
  1012. av_free(pkt.side_data[i].data);
  1013. av_freep(&pkt.side_data);
  1014. pkt.side_data_elems = 0;
  1015. }
  1016. return ret ? ret : pkt.size;
  1017. }
  1018. #endif
  1019. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1020. AVPacket *avpkt,
  1021. const AVFrame *frame,
  1022. int *got_packet_ptr)
  1023. {
  1024. int ret;
  1025. int user_packet = !!avpkt->data;
  1026. *got_packet_ptr = 0;
  1027. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1028. av_free_packet(avpkt);
  1029. av_init_packet(avpkt);
  1030. avpkt->size = 0;
  1031. return 0;
  1032. }
  1033. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1034. return AVERROR(EINVAL);
  1035. av_assert0(avctx->codec->encode2);
  1036. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1037. if (!ret) {
  1038. if (!*got_packet_ptr)
  1039. avpkt->size = 0;
  1040. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1041. avpkt->pts = avpkt->dts = frame->pts;
  1042. if (!user_packet && avpkt->size) {
  1043. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  1044. if (new_data)
  1045. avpkt->data = new_data;
  1046. }
  1047. avctx->frame_number++;
  1048. }
  1049. if (ret < 0 || !*got_packet_ptr)
  1050. av_free_packet(avpkt);
  1051. emms_c();
  1052. return ret;
  1053. }
  1054. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1055. const AVSubtitle *sub)
  1056. {
  1057. int ret;
  1058. if (sub->start_display_time) {
  1059. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1060. return -1;
  1061. }
  1062. if (sub->num_rects == 0 || !sub->rects)
  1063. return -1;
  1064. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1065. avctx->frame_number++;
  1066. return ret;
  1067. }
  1068. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1069. {
  1070. int size = 0;
  1071. const uint8_t *data;
  1072. uint32_t flags;
  1073. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1074. return;
  1075. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1076. if (!data || size < 4)
  1077. return;
  1078. flags = bytestream_get_le32(&data);
  1079. size -= 4;
  1080. if (size < 4) /* Required for any of the changes */
  1081. return;
  1082. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1083. avctx->channels = bytestream_get_le32(&data);
  1084. size -= 4;
  1085. }
  1086. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1087. if (size < 8)
  1088. return;
  1089. avctx->channel_layout = bytestream_get_le64(&data);
  1090. size -= 8;
  1091. }
  1092. if (size < 4)
  1093. return;
  1094. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1095. avctx->sample_rate = bytestream_get_le32(&data);
  1096. size -= 4;
  1097. }
  1098. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1099. if (size < 8)
  1100. return;
  1101. avctx->width = bytestream_get_le32(&data);
  1102. avctx->height = bytestream_get_le32(&data);
  1103. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1104. size -= 8;
  1105. }
  1106. }
  1107. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1108. int *got_picture_ptr,
  1109. AVPacket *avpkt)
  1110. {
  1111. int ret;
  1112. *got_picture_ptr = 0;
  1113. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1114. return -1;
  1115. avctx->pkt = avpkt;
  1116. apply_param_change(avctx, avpkt);
  1117. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1118. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1119. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1120. avpkt);
  1121. else {
  1122. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1123. avpkt);
  1124. picture->pkt_dts = avpkt->dts;
  1125. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1126. picture->width = avctx->width;
  1127. picture->height = avctx->height;
  1128. picture->format = avctx->pix_fmt;
  1129. }
  1130. emms_c(); //needed to avoid an emms_c() call before every return;
  1131. if (*got_picture_ptr)
  1132. avctx->frame_number++;
  1133. } else
  1134. ret = 0;
  1135. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1136. * make sure it's set correctly */
  1137. picture->extended_data = picture->data;
  1138. return ret;
  1139. }
  1140. #if FF_API_OLD_DECODE_AUDIO
  1141. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1142. int *frame_size_ptr,
  1143. AVPacket *avpkt)
  1144. {
  1145. AVFrame frame;
  1146. int ret, got_frame = 0;
  1147. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1148. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1149. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1150. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1151. "avcodec_decode_audio4()\n");
  1152. avctx->get_buffer = avcodec_default_get_buffer;
  1153. }
  1154. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1155. if (ret >= 0 && got_frame) {
  1156. int ch, plane_size;
  1157. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1158. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1159. frame.nb_samples,
  1160. avctx->sample_fmt, 1);
  1161. if (*frame_size_ptr < data_size) {
  1162. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1163. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1164. return AVERROR(EINVAL);
  1165. }
  1166. memcpy(samples, frame.extended_data[0], plane_size);
  1167. if (planar && avctx->channels > 1) {
  1168. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1169. for (ch = 1; ch < avctx->channels; ch++) {
  1170. memcpy(out, frame.extended_data[ch], plane_size);
  1171. out += plane_size;
  1172. }
  1173. }
  1174. *frame_size_ptr = data_size;
  1175. } else {
  1176. *frame_size_ptr = 0;
  1177. }
  1178. return ret;
  1179. }
  1180. #endif
  1181. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1182. AVFrame *frame,
  1183. int *got_frame_ptr,
  1184. AVPacket *avpkt)
  1185. {
  1186. int planar, channels;
  1187. int ret = 0;
  1188. *got_frame_ptr = 0;
  1189. avctx->pkt = avpkt;
  1190. if (!avpkt->data && avpkt->size) {
  1191. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1192. return AVERROR(EINVAL);
  1193. }
  1194. apply_param_change(avctx, avpkt);
  1195. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1196. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1197. if (ret >= 0 && *got_frame_ptr) {
  1198. avctx->frame_number++;
  1199. frame->pkt_dts = avpkt->dts;
  1200. if (frame->format == AV_SAMPLE_FMT_NONE)
  1201. frame->format = avctx->sample_fmt;
  1202. }
  1203. }
  1204. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1205. * make sure it's set correctly; assume decoders that actually use
  1206. * extended_data are doing it correctly */
  1207. planar = av_sample_fmt_is_planar(frame->format);
  1208. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1209. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1210. frame->extended_data = frame->data;
  1211. return ret;
  1212. }
  1213. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1214. int *got_sub_ptr,
  1215. AVPacket *avpkt)
  1216. {
  1217. int ret;
  1218. avctx->pkt = avpkt;
  1219. *got_sub_ptr = 0;
  1220. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1221. if (*got_sub_ptr)
  1222. avctx->frame_number++;
  1223. return ret;
  1224. }
  1225. void avsubtitle_free(AVSubtitle *sub)
  1226. {
  1227. int i;
  1228. for (i = 0; i < sub->num_rects; i++) {
  1229. av_freep(&sub->rects[i]->pict.data[0]);
  1230. av_freep(&sub->rects[i]->pict.data[1]);
  1231. av_freep(&sub->rects[i]->pict.data[2]);
  1232. av_freep(&sub->rects[i]->pict.data[3]);
  1233. av_freep(&sub->rects[i]->text);
  1234. av_freep(&sub->rects[i]->ass);
  1235. av_freep(&sub->rects[i]);
  1236. }
  1237. av_freep(&sub->rects);
  1238. memset(sub, 0, sizeof(AVSubtitle));
  1239. }
  1240. av_cold int avcodec_close(AVCodecContext *avctx)
  1241. {
  1242. /* If there is a user-supplied mutex locking routine, call it. */
  1243. if (ff_lockmgr_cb) {
  1244. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1245. return -1;
  1246. }
  1247. entangled_thread_counter++;
  1248. if (entangled_thread_counter != 1) {
  1249. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1250. entangled_thread_counter--;
  1251. return -1;
  1252. }
  1253. if (avcodec_is_open(avctx)) {
  1254. if (HAVE_THREADS && avctx->thread_opaque)
  1255. ff_thread_free(avctx);
  1256. if (avctx->codec && avctx->codec->close)
  1257. avctx->codec->close(avctx);
  1258. avcodec_default_free_buffers(avctx);
  1259. avctx->coded_frame = NULL;
  1260. av_freep(&avctx->internal);
  1261. }
  1262. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1263. av_opt_free(avctx->priv_data);
  1264. av_opt_free(avctx);
  1265. av_freep(&avctx->priv_data);
  1266. if (av_codec_is_encoder(avctx->codec))
  1267. av_freep(&avctx->extradata);
  1268. avctx->codec = NULL;
  1269. avctx->active_thread_type = 0;
  1270. entangled_thread_counter--;
  1271. /* Release any user-supplied mutex. */
  1272. if (ff_lockmgr_cb) {
  1273. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1274. }
  1275. return 0;
  1276. }
  1277. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1278. {
  1279. AVCodec *p, *experimental = NULL;
  1280. p = first_avcodec;
  1281. while (p) {
  1282. if (av_codec_is_encoder(p) && p->id == id) {
  1283. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1284. experimental = p;
  1285. } else
  1286. return p;
  1287. }
  1288. p = p->next;
  1289. }
  1290. return experimental;
  1291. }
  1292. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1293. {
  1294. AVCodec *p;
  1295. if (!name)
  1296. return NULL;
  1297. p = first_avcodec;
  1298. while (p) {
  1299. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1300. return p;
  1301. p = p->next;
  1302. }
  1303. return NULL;
  1304. }
  1305. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1306. {
  1307. AVCodec *p;
  1308. p = first_avcodec;
  1309. while (p) {
  1310. if (av_codec_is_decoder(p) && p->id == id)
  1311. return p;
  1312. p = p->next;
  1313. }
  1314. return NULL;
  1315. }
  1316. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1317. {
  1318. AVCodec *p;
  1319. if (!name)
  1320. return NULL;
  1321. p = first_avcodec;
  1322. while (p) {
  1323. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1324. return p;
  1325. p = p->next;
  1326. }
  1327. return NULL;
  1328. }
  1329. static int get_bit_rate(AVCodecContext *ctx)
  1330. {
  1331. int bit_rate;
  1332. int bits_per_sample;
  1333. switch (ctx->codec_type) {
  1334. case AVMEDIA_TYPE_VIDEO:
  1335. case AVMEDIA_TYPE_DATA:
  1336. case AVMEDIA_TYPE_SUBTITLE:
  1337. case AVMEDIA_TYPE_ATTACHMENT:
  1338. bit_rate = ctx->bit_rate;
  1339. break;
  1340. case AVMEDIA_TYPE_AUDIO:
  1341. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1342. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1343. break;
  1344. default:
  1345. bit_rate = 0;
  1346. break;
  1347. }
  1348. return bit_rate;
  1349. }
  1350. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1351. {
  1352. int i, len, ret = 0;
  1353. for (i = 0; i < 4; i++) {
  1354. len = snprintf(buf, buf_size,
  1355. isprint(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1356. buf += len;
  1357. buf_size = buf_size > len ? buf_size - len : 0;
  1358. ret += len;
  1359. codec_tag >>= 8;
  1360. }
  1361. return ret;
  1362. }
  1363. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1364. {
  1365. const char *codec_name;
  1366. const char *profile = NULL;
  1367. const AVCodec *p;
  1368. char buf1[32];
  1369. int bitrate;
  1370. AVRational display_aspect_ratio;
  1371. if (enc->codec)
  1372. p = enc->codec;
  1373. else if (encode)
  1374. p = avcodec_find_encoder(enc->codec_id);
  1375. else
  1376. p = avcodec_find_decoder(enc->codec_id);
  1377. if (p) {
  1378. codec_name = p->name;
  1379. profile = av_get_profile_name(p, enc->profile);
  1380. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1381. /* fake mpeg2 transport stream codec (currently not
  1382. * registered) */
  1383. codec_name = "mpeg2ts";
  1384. } else if (enc->codec_name[0] != '\0') {
  1385. codec_name = enc->codec_name;
  1386. } else {
  1387. /* output avi tags */
  1388. char tag_buf[32];
  1389. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1390. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1391. codec_name = buf1;
  1392. }
  1393. switch (enc->codec_type) {
  1394. case AVMEDIA_TYPE_VIDEO:
  1395. snprintf(buf, buf_size,
  1396. "Video: %s%s",
  1397. codec_name, enc->mb_decision ? " (hq)" : "");
  1398. if (profile)
  1399. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1400. " (%s)", profile);
  1401. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1402. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1403. ", %s",
  1404. av_get_pix_fmt_name(enc->pix_fmt));
  1405. }
  1406. if (enc->width) {
  1407. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1408. ", %dx%d",
  1409. enc->width, enc->height);
  1410. if (enc->sample_aspect_ratio.num) {
  1411. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1412. enc->width * enc->sample_aspect_ratio.num,
  1413. enc->height * enc->sample_aspect_ratio.den,
  1414. 1024 * 1024);
  1415. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1416. " [PAR %d:%d DAR %d:%d]",
  1417. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1418. display_aspect_ratio.num, display_aspect_ratio.den);
  1419. }
  1420. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1421. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1422. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1423. ", %d/%d",
  1424. enc->time_base.num / g, enc->time_base.den / g);
  1425. }
  1426. }
  1427. if (encode) {
  1428. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1429. ", q=%d-%d", enc->qmin, enc->qmax);
  1430. }
  1431. break;
  1432. case AVMEDIA_TYPE_AUDIO:
  1433. snprintf(buf, buf_size,
  1434. "Audio: %s",
  1435. codec_name);
  1436. if (profile)
  1437. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1438. " (%s)", profile);
  1439. if (enc->sample_rate) {
  1440. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1441. ", %d Hz", enc->sample_rate);
  1442. }
  1443. av_strlcat(buf, ", ", buf_size);
  1444. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1445. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1446. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1447. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1448. }
  1449. break;
  1450. case AVMEDIA_TYPE_DATA:
  1451. snprintf(buf, buf_size, "Data: %s", codec_name);
  1452. break;
  1453. case AVMEDIA_TYPE_SUBTITLE:
  1454. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1455. break;
  1456. case AVMEDIA_TYPE_ATTACHMENT:
  1457. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1458. break;
  1459. default:
  1460. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1461. return;
  1462. }
  1463. if (encode) {
  1464. if (enc->flags & CODEC_FLAG_PASS1)
  1465. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1466. ", pass 1");
  1467. if (enc->flags & CODEC_FLAG_PASS2)
  1468. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1469. ", pass 2");
  1470. }
  1471. bitrate = get_bit_rate(enc);
  1472. if (bitrate != 0) {
  1473. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1474. ", %d kb/s", bitrate / 1000);
  1475. }
  1476. }
  1477. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1478. {
  1479. const AVProfile *p;
  1480. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1481. return NULL;
  1482. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1483. if (p->profile == profile)
  1484. return p->name;
  1485. return NULL;
  1486. }
  1487. unsigned avcodec_version(void)
  1488. {
  1489. return LIBAVCODEC_VERSION_INT;
  1490. }
  1491. const char *avcodec_configuration(void)
  1492. {
  1493. return LIBAV_CONFIGURATION;
  1494. }
  1495. const char *avcodec_license(void)
  1496. {
  1497. #define LICENSE_PREFIX "libavcodec license: "
  1498. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1499. }
  1500. void avcodec_flush_buffers(AVCodecContext *avctx)
  1501. {
  1502. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1503. ff_thread_flush(avctx);
  1504. else if (avctx->codec->flush)
  1505. avctx->codec->flush(avctx);
  1506. }
  1507. static void video_free_buffers(AVCodecContext *s)
  1508. {
  1509. AVCodecInternal *avci = s->internal;
  1510. int i, j;
  1511. if (!avci->buffer)
  1512. return;
  1513. if (avci->buffer_count)
  1514. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1515. avci->buffer_count);
  1516. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1517. InternalBuffer *buf = &avci->buffer[i];
  1518. for (j = 0; j < 4; j++) {
  1519. av_freep(&buf->base[j]);
  1520. buf->data[j] = NULL;
  1521. }
  1522. }
  1523. av_freep(&avci->buffer);
  1524. avci->buffer_count = 0;
  1525. }
  1526. static void audio_free_buffers(AVCodecContext *avctx)
  1527. {
  1528. AVCodecInternal *avci = avctx->internal;
  1529. InternalBuffer *buf;
  1530. if (!avci->buffer)
  1531. return;
  1532. buf = avci->buffer;
  1533. if (buf->extended_data) {
  1534. av_free(buf->extended_data[0]);
  1535. if (buf->extended_data != buf->data)
  1536. av_free(buf->extended_data);
  1537. }
  1538. av_freep(&avci->buffer);
  1539. }
  1540. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1541. {
  1542. switch (avctx->codec_type) {
  1543. case AVMEDIA_TYPE_VIDEO:
  1544. video_free_buffers(avctx);
  1545. break;
  1546. case AVMEDIA_TYPE_AUDIO:
  1547. audio_free_buffers(avctx);
  1548. break;
  1549. default:
  1550. break;
  1551. }
  1552. }
  1553. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1554. {
  1555. switch (codec_id) {
  1556. case AV_CODEC_ID_ADPCM_CT:
  1557. case AV_CODEC_ID_ADPCM_IMA_APC:
  1558. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1559. case AV_CODEC_ID_ADPCM_IMA_WS:
  1560. case AV_CODEC_ID_ADPCM_G722:
  1561. case AV_CODEC_ID_ADPCM_YAMAHA:
  1562. return 4;
  1563. case AV_CODEC_ID_PCM_ALAW:
  1564. case AV_CODEC_ID_PCM_MULAW:
  1565. case AV_CODEC_ID_PCM_S8:
  1566. case AV_CODEC_ID_PCM_U8:
  1567. case AV_CODEC_ID_PCM_ZORK:
  1568. return 8;
  1569. case AV_CODEC_ID_PCM_S16BE:
  1570. case AV_CODEC_ID_PCM_S16LE:
  1571. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1572. case AV_CODEC_ID_PCM_U16BE:
  1573. case AV_CODEC_ID_PCM_U16LE:
  1574. return 16;
  1575. case AV_CODEC_ID_PCM_S24DAUD:
  1576. case AV_CODEC_ID_PCM_S24BE:
  1577. case AV_CODEC_ID_PCM_S24LE:
  1578. case AV_CODEC_ID_PCM_U24BE:
  1579. case AV_CODEC_ID_PCM_U24LE:
  1580. return 24;
  1581. case AV_CODEC_ID_PCM_S32BE:
  1582. case AV_CODEC_ID_PCM_S32LE:
  1583. case AV_CODEC_ID_PCM_U32BE:
  1584. case AV_CODEC_ID_PCM_U32LE:
  1585. case AV_CODEC_ID_PCM_F32BE:
  1586. case AV_CODEC_ID_PCM_F32LE:
  1587. return 32;
  1588. case AV_CODEC_ID_PCM_F64BE:
  1589. case AV_CODEC_ID_PCM_F64LE:
  1590. return 64;
  1591. default:
  1592. return 0;
  1593. }
  1594. }
  1595. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1596. {
  1597. switch (codec_id) {
  1598. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1599. return 2;
  1600. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1601. return 3;
  1602. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1603. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1604. case AV_CODEC_ID_ADPCM_IMA_QT:
  1605. case AV_CODEC_ID_ADPCM_SWF:
  1606. case AV_CODEC_ID_ADPCM_MS:
  1607. return 4;
  1608. default:
  1609. return av_get_exact_bits_per_sample(codec_id);
  1610. }
  1611. }
  1612. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1613. {
  1614. int id, sr, ch, ba, tag, bps;
  1615. id = avctx->codec_id;
  1616. sr = avctx->sample_rate;
  1617. ch = avctx->channels;
  1618. ba = avctx->block_align;
  1619. tag = avctx->codec_tag;
  1620. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1621. /* codecs with an exact constant bits per sample */
  1622. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1623. return (frame_bytes * 8) / (bps * ch);
  1624. bps = avctx->bits_per_coded_sample;
  1625. /* codecs with a fixed packet duration */
  1626. switch (id) {
  1627. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1628. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1629. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1630. case AV_CODEC_ID_AMR_NB:
  1631. case AV_CODEC_ID_GSM:
  1632. case AV_CODEC_ID_QCELP:
  1633. case AV_CODEC_ID_RA_144:
  1634. case AV_CODEC_ID_RA_288: return 160;
  1635. case AV_CODEC_ID_IMC: return 256;
  1636. case AV_CODEC_ID_AMR_WB:
  1637. case AV_CODEC_ID_GSM_MS: return 320;
  1638. case AV_CODEC_ID_MP1: return 384;
  1639. case AV_CODEC_ID_ATRAC1: return 512;
  1640. case AV_CODEC_ID_ATRAC3: return 1024;
  1641. case AV_CODEC_ID_MP2:
  1642. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1643. case AV_CODEC_ID_AC3: return 1536;
  1644. }
  1645. if (sr > 0) {
  1646. /* calc from sample rate */
  1647. if (id == AV_CODEC_ID_TTA)
  1648. return 256 * sr / 245;
  1649. if (ch > 0) {
  1650. /* calc from sample rate and channels */
  1651. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1652. return (480 << (sr / 22050)) / ch;
  1653. }
  1654. }
  1655. if (ba > 0) {
  1656. /* calc from block_align */
  1657. if (id == AV_CODEC_ID_SIPR) {
  1658. switch (ba) {
  1659. case 20: return 160;
  1660. case 19: return 144;
  1661. case 29: return 288;
  1662. case 37: return 480;
  1663. }
  1664. } else if (id == AV_CODEC_ID_ILBC) {
  1665. switch (ba) {
  1666. case 38: return 160;
  1667. case 50: return 240;
  1668. }
  1669. }
  1670. }
  1671. if (frame_bytes > 0) {
  1672. /* calc from frame_bytes only */
  1673. if (id == AV_CODEC_ID_TRUESPEECH)
  1674. return 240 * (frame_bytes / 32);
  1675. if (id == AV_CODEC_ID_NELLYMOSER)
  1676. return 256 * (frame_bytes / 64);
  1677. if (bps > 0) {
  1678. /* calc from frame_bytes and bits_per_coded_sample */
  1679. if (id == AV_CODEC_ID_ADPCM_G726)
  1680. return frame_bytes * 8 / bps;
  1681. }
  1682. if (ch > 0) {
  1683. /* calc from frame_bytes and channels */
  1684. switch (id) {
  1685. case AV_CODEC_ID_ADPCM_4XM:
  1686. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1687. return (frame_bytes - 4 * ch) * 2 / ch;
  1688. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1689. return (frame_bytes - 4) * 2 / ch;
  1690. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1691. return (frame_bytes - 8) * 2 / ch;
  1692. case AV_CODEC_ID_ADPCM_XA:
  1693. return (frame_bytes / 128) * 224 / ch;
  1694. case AV_CODEC_ID_INTERPLAY_DPCM:
  1695. return (frame_bytes - 6 - ch) / ch;
  1696. case AV_CODEC_ID_ROQ_DPCM:
  1697. return (frame_bytes - 8) / ch;
  1698. case AV_CODEC_ID_XAN_DPCM:
  1699. return (frame_bytes - 2 * ch) / ch;
  1700. case AV_CODEC_ID_MACE3:
  1701. return 3 * frame_bytes / ch;
  1702. case AV_CODEC_ID_MACE6:
  1703. return 6 * frame_bytes / ch;
  1704. case AV_CODEC_ID_PCM_LXF:
  1705. return 2 * (frame_bytes / (5 * ch));
  1706. }
  1707. if (tag) {
  1708. /* calc from frame_bytes, channels, and codec_tag */
  1709. if (id == AV_CODEC_ID_SOL_DPCM) {
  1710. if (tag == 3)
  1711. return frame_bytes / ch;
  1712. else
  1713. return frame_bytes * 2 / ch;
  1714. }
  1715. }
  1716. if (ba > 0) {
  1717. /* calc from frame_bytes, channels, and block_align */
  1718. int blocks = frame_bytes / ba;
  1719. switch (avctx->codec_id) {
  1720. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1721. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1722. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1723. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1724. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1725. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1726. case AV_CODEC_ID_ADPCM_MS:
  1727. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1728. }
  1729. }
  1730. if (bps > 0) {
  1731. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1732. switch (avctx->codec_id) {
  1733. case AV_CODEC_ID_PCM_DVD:
  1734. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1735. case AV_CODEC_ID_PCM_BLURAY:
  1736. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1737. case AV_CODEC_ID_S302M:
  1738. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1739. }
  1740. }
  1741. }
  1742. }
  1743. return 0;
  1744. }
  1745. #if !HAVE_THREADS
  1746. int ff_thread_init(AVCodecContext *s)
  1747. {
  1748. return -1;
  1749. }
  1750. #endif
  1751. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1752. {
  1753. unsigned int n = 0;
  1754. while (v >= 0xff) {
  1755. *s++ = 0xff;
  1756. v -= 0xff;
  1757. n++;
  1758. }
  1759. *s = v;
  1760. n++;
  1761. return n;
  1762. }
  1763. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1764. {
  1765. int i;
  1766. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1767. return i;
  1768. }
  1769. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1770. {
  1771. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1772. "version to the newest one from Git. If the problem still "
  1773. "occurs, it means that your file has a feature which has not "
  1774. "been implemented.\n", feature);
  1775. if(want_sample)
  1776. av_log_ask_for_sample(avc, NULL);
  1777. }
  1778. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1779. {
  1780. va_list argument_list;
  1781. va_start(argument_list, msg);
  1782. if (msg)
  1783. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1784. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1785. "of this file to ftp://upload.libav.org/incoming/ "
  1786. "and contact the libav-devel mailing list.\n");
  1787. va_end(argument_list);
  1788. }
  1789. static AVHWAccel *first_hwaccel = NULL;
  1790. void av_register_hwaccel(AVHWAccel *hwaccel)
  1791. {
  1792. AVHWAccel **p = &first_hwaccel;
  1793. while (*p)
  1794. p = &(*p)->next;
  1795. *p = hwaccel;
  1796. hwaccel->next = NULL;
  1797. }
  1798. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1799. {
  1800. return hwaccel ? hwaccel->next : first_hwaccel;
  1801. }
  1802. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  1803. {
  1804. AVHWAccel *hwaccel = NULL;
  1805. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1806. if (hwaccel->id == codec_id
  1807. && hwaccel->pix_fmt == pix_fmt)
  1808. return hwaccel;
  1809. return NULL;
  1810. }
  1811. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1812. {
  1813. if (ff_lockmgr_cb) {
  1814. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1815. return -1;
  1816. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1817. return -1;
  1818. }
  1819. ff_lockmgr_cb = cb;
  1820. if (ff_lockmgr_cb) {
  1821. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1822. return -1;
  1823. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1824. return -1;
  1825. }
  1826. return 0;
  1827. }
  1828. int avpriv_lock_avformat(void)
  1829. {
  1830. if (ff_lockmgr_cb) {
  1831. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1832. return -1;
  1833. }
  1834. return 0;
  1835. }
  1836. int avpriv_unlock_avformat(void)
  1837. {
  1838. if (ff_lockmgr_cb) {
  1839. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1840. return -1;
  1841. }
  1842. return 0;
  1843. }
  1844. unsigned int avpriv_toupper4(unsigned int x)
  1845. {
  1846. return toupper(x & 0xFF)
  1847. + (toupper((x >> 8) & 0xFF) << 8)
  1848. + (toupper((x >> 16) & 0xFF) << 16)
  1849. + (toupper((x >> 24) & 0xFF) << 24);
  1850. }
  1851. #if !HAVE_THREADS
  1852. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1853. {
  1854. f->owner = avctx;
  1855. return avctx->get_buffer(avctx, f);
  1856. }
  1857. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1858. {
  1859. f->owner->release_buffer(f->owner, f);
  1860. }
  1861. void ff_thread_finish_setup(AVCodecContext *avctx)
  1862. {
  1863. }
  1864. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1865. {
  1866. }
  1867. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1868. {
  1869. }
  1870. #endif
  1871. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1872. {
  1873. if (codec_id <= AV_CODEC_ID_NONE)
  1874. return AVMEDIA_TYPE_UNKNOWN;
  1875. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1876. return AVMEDIA_TYPE_VIDEO;
  1877. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1878. return AVMEDIA_TYPE_AUDIO;
  1879. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1880. return AVMEDIA_TYPE_SUBTITLE;
  1881. return AVMEDIA_TYPE_UNKNOWN;
  1882. }
  1883. int avcodec_is_open(AVCodecContext *s)
  1884. {
  1885. return !!s->internal;
  1886. }