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.

2181 lines
66KB

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