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.

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