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.

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