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.

2094 lines
63KB

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