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.

2012 lines
62KB

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