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.

2081 lines
64KB

  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->pix_fmts) {
  718. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  719. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  720. break;
  721. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
  722. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  723. ret = AVERROR(EINVAL);
  724. goto free_and_end;
  725. }
  726. }
  727. if (avctx->codec->supported_samplerates) {
  728. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  729. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  730. break;
  731. if (avctx->codec->supported_samplerates[i] == 0) {
  732. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  733. ret = AVERROR(EINVAL);
  734. goto free_and_end;
  735. }
  736. }
  737. if (avctx->codec->channel_layouts) {
  738. if (!avctx->channel_layout) {
  739. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  740. } else {
  741. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  742. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  743. break;
  744. if (avctx->codec->channel_layouts[i] == 0) {
  745. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  746. ret = AVERROR(EINVAL);
  747. goto free_and_end;
  748. }
  749. }
  750. }
  751. if (avctx->channel_layout && avctx->channels) {
  752. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  753. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  754. ret = AVERROR(EINVAL);
  755. goto free_and_end;
  756. }
  757. } else if (avctx->channel_layout) {
  758. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  759. }
  760. }
  761. avctx->pts_correction_num_faulty_pts =
  762. avctx->pts_correction_num_faulty_dts = 0;
  763. avctx->pts_correction_last_pts =
  764. avctx->pts_correction_last_dts = INT64_MIN;
  765. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  766. ret = avctx->codec->init(avctx);
  767. if (ret < 0) {
  768. goto free_and_end;
  769. }
  770. }
  771. ret=0;
  772. end:
  773. entangled_thread_counter--;
  774. /* Release any user-supplied mutex. */
  775. if (ff_lockmgr_cb) {
  776. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  777. }
  778. if (options) {
  779. av_dict_free(options);
  780. *options = tmp;
  781. }
  782. return ret;
  783. free_and_end:
  784. av_dict_free(&tmp);
  785. av_freep(&avctx->priv_data);
  786. av_freep(&avctx->internal);
  787. avctx->codec= NULL;
  788. goto end;
  789. }
  790. int ff_alloc_packet(AVPacket *avpkt, int size)
  791. {
  792. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  793. return AVERROR(EINVAL);
  794. if (avpkt->data) {
  795. uint8_t *pkt_data;
  796. if (avpkt->size < size)
  797. return AVERROR(EINVAL);
  798. pkt_data = avpkt->data;
  799. av_init_packet(avpkt);
  800. avpkt->data = pkt_data;
  801. avpkt->size = size;
  802. return 0;
  803. } else {
  804. return av_new_packet(avpkt, size);
  805. }
  806. }
  807. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  808. AVPacket *avpkt,
  809. const AVFrame *frame,
  810. int *got_packet_ptr)
  811. {
  812. int ret;
  813. int user_packet = !!avpkt->data;
  814. int nb_samples;
  815. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  816. av_init_packet(avpkt);
  817. avpkt->size = 0;
  818. return 0;
  819. }
  820. /* check for valid frame size */
  821. if (frame) {
  822. nb_samples = frame->nb_samples;
  823. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  824. if (nb_samples > avctx->frame_size)
  825. return AVERROR(EINVAL);
  826. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  827. if (nb_samples != avctx->frame_size)
  828. return AVERROR(EINVAL);
  829. }
  830. } else {
  831. nb_samples = avctx->frame_size;
  832. }
  833. if (avctx->codec->encode2) {
  834. *got_packet_ptr = 0;
  835. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  836. if (!ret && *got_packet_ptr) {
  837. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  838. avpkt->pts = frame->pts;
  839. avpkt->duration = av_rescale_q(frame->nb_samples,
  840. (AVRational){ 1, avctx->sample_rate },
  841. avctx->time_base);
  842. }
  843. avpkt->dts = avpkt->pts;
  844. } else {
  845. avpkt->size = 0;
  846. }
  847. } else {
  848. /* for compatibility with encoders not supporting encode2(), we need to
  849. allocate a packet buffer if the user has not provided one or check
  850. the size otherwise */
  851. int fs_tmp = 0;
  852. int buf_size = avpkt->size;
  853. if (!user_packet) {
  854. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  855. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  856. if (!frame)
  857. return AVERROR(EINVAL);
  858. buf_size = nb_samples * avctx->channels *
  859. av_get_bits_per_sample(avctx->codec_id) / 8;
  860. } else {
  861. /* this is a guess as to the required size.
  862. if an encoder needs more than this, it should probably
  863. implement encode2() */
  864. buf_size = 2 * avctx->frame_size * avctx->channels *
  865. av_get_bytes_per_sample(avctx->sample_fmt);
  866. buf_size += FF_MIN_BUFFER_SIZE;
  867. }
  868. }
  869. if ((ret = ff_alloc_packet(avpkt, buf_size)))
  870. return ret;
  871. /* Encoders using AVCodec.encode() that support
  872. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  873. the smaller size when encoding the last frame.
  874. This code can be removed once all encoders supporting
  875. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  876. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  877. nb_samples < avctx->frame_size) {
  878. fs_tmp = avctx->frame_size;
  879. avctx->frame_size = nb_samples;
  880. }
  881. /* encode the frame */
  882. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  883. frame ? frame->data[0] : NULL);
  884. if (ret >= 0) {
  885. if (!ret) {
  886. /* no output. if the packet data was allocated by libavcodec,
  887. free it */
  888. if (!user_packet)
  889. av_freep(&avpkt->data);
  890. } else {
  891. if (avctx->coded_frame)
  892. avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
  893. /* Set duration for final small packet. This can be removed
  894. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  895. encode2() */
  896. if (fs_tmp) {
  897. avpkt->duration = av_rescale_q(avctx->frame_size,
  898. (AVRational){ 1, avctx->sample_rate },
  899. avctx->time_base);
  900. }
  901. }
  902. avpkt->size = ret;
  903. *got_packet_ptr = (ret > 0);
  904. ret = 0;
  905. }
  906. if (fs_tmp)
  907. avctx->frame_size = fs_tmp;
  908. }
  909. if (!ret)
  910. avctx->frame_number++;
  911. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  912. this needs to be moved to the encoders, but for now we can do it
  913. here to simplify things */
  914. avpkt->flags |= AV_PKT_FLAG_KEY;
  915. return ret;
  916. }
  917. #if FF_API_OLD_DECODE_AUDIO
  918. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  919. uint8_t *buf, int buf_size,
  920. const short *samples)
  921. {
  922. AVPacket pkt;
  923. AVFrame frame0;
  924. AVFrame *frame;
  925. int ret, samples_size, got_packet;
  926. av_init_packet(&pkt);
  927. pkt.data = buf;
  928. pkt.size = buf_size;
  929. if (samples) {
  930. frame = &frame0;
  931. avcodec_get_frame_defaults(frame);
  932. if (avctx->frame_size) {
  933. frame->nb_samples = avctx->frame_size;
  934. } else {
  935. /* if frame_size is not set, the number of samples must be
  936. calculated from the buffer size */
  937. int64_t nb_samples;
  938. if (!av_get_bits_per_sample(avctx->codec_id)) {
  939. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  940. "support this codec\n");
  941. return AVERROR(EINVAL);
  942. }
  943. nb_samples = (int64_t)buf_size * 8 /
  944. (av_get_bits_per_sample(avctx->codec_id) *
  945. avctx->channels);
  946. if (nb_samples >= INT_MAX)
  947. return AVERROR(EINVAL);
  948. frame->nb_samples = nb_samples;
  949. }
  950. /* it is assumed that the samples buffer is large enough based on the
  951. relevant parameters */
  952. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  953. frame->nb_samples,
  954. avctx->sample_fmt, 1);
  955. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  956. avctx->sample_fmt,
  957. samples, samples_size, 1)))
  958. return ret;
  959. /* fabricate frame pts from sample count.
  960. this is needed because the avcodec_encode_audio() API does not have
  961. a way for the user to provide pts */
  962. if(avctx->sample_rate && avctx->time_base.num)
  963. frame->pts = av_rescale_q(avctx->internal->sample_count,
  964. (AVRational){ 1, avctx->sample_rate },
  965. avctx->time_base);
  966. else
  967. frame->pts = AV_NOPTS_VALUE;
  968. avctx->internal->sample_count += frame->nb_samples;
  969. } else {
  970. frame = NULL;
  971. }
  972. got_packet = 0;
  973. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  974. if (!ret && got_packet && avctx->coded_frame) {
  975. avctx->coded_frame->pts = pkt.pts;
  976. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  977. }
  978. /* free any side data since we cannot return it */
  979. ff_packet_free_side_data(&pkt);
  980. if (frame && frame->extended_data != frame->data)
  981. av_freep(&frame->extended_data);
  982. return ret ? ret : pkt.size;
  983. }
  984. #endif
  985. #if FF_API_OLD_ENCODE_VIDEO
  986. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  987. const AVFrame *pict)
  988. {
  989. AVPacket pkt;
  990. int ret, got_packet = 0;
  991. if(buf_size < FF_MIN_BUFFER_SIZE){
  992. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  993. return -1;
  994. }
  995. av_init_packet(&pkt);
  996. pkt.data = buf;
  997. pkt.size = buf_size;
  998. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  999. if (!ret && got_packet && avctx->coded_frame) {
  1000. avctx->coded_frame->pts = pkt.pts;
  1001. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1002. }
  1003. /* free any side data since we cannot return it */
  1004. if (pkt.side_data_elems > 0) {
  1005. int i;
  1006. for (i = 0; i < pkt.side_data_elems; i++)
  1007. av_free(pkt.side_data[i].data);
  1008. av_freep(&pkt.side_data);
  1009. pkt.side_data_elems = 0;
  1010. }
  1011. return ret ? ret : pkt.size;
  1012. }
  1013. #endif
  1014. #define MAX_CODED_FRAME_SIZE(width, height)\
  1015. (9*(width)*(height) + FF_MIN_BUFFER_SIZE)
  1016. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1017. AVPacket *avpkt,
  1018. const AVFrame *frame,
  1019. int *got_packet_ptr)
  1020. {
  1021. int ret;
  1022. int user_packet = !!avpkt->data;
  1023. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1024. av_init_packet(avpkt);
  1025. avpkt->size = 0;
  1026. *got_packet_ptr = 0;
  1027. return 0;
  1028. }
  1029. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1030. return AVERROR(EINVAL);
  1031. if (avctx->codec->encode2) {
  1032. *got_packet_ptr = 0;
  1033. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1034. if (!ret) {
  1035. if (!*got_packet_ptr)
  1036. avpkt->size = 0;
  1037. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1038. avpkt->pts = avpkt->dts = frame->pts;
  1039. }
  1040. } else {
  1041. /* for compatibility with encoders not supporting encode2(), we need to
  1042. allocate a packet buffer if the user has not provided one or check
  1043. the size otherwise */
  1044. int buf_size = avpkt->size;
  1045. if (!user_packet)
  1046. buf_size = MAX_CODED_FRAME_SIZE(avctx->width, avctx->height);
  1047. if ((ret = ff_alloc_packet(avpkt, buf_size)))
  1048. return ret;
  1049. /* encode the frame */
  1050. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size, frame);
  1051. if (ret >= 0) {
  1052. if (!ret) {
  1053. /* no output. if the packet data was allocated by libavcodec,
  1054. free it */
  1055. if (!user_packet)
  1056. av_freep(&avpkt->data);
  1057. } else if (avctx->coded_frame) {
  1058. avpkt->pts = avctx->coded_frame->pts;
  1059. avpkt->flags |= AV_PKT_FLAG_KEY*!!avctx->coded_frame->key_frame;
  1060. }
  1061. avpkt->size = ret;
  1062. *got_packet_ptr = (ret > 0);
  1063. ret = 0;
  1064. }
  1065. }
  1066. if (!ret)
  1067. avctx->frame_number++;
  1068. emms_c();
  1069. return ret;
  1070. }
  1071. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1072. const AVSubtitle *sub)
  1073. {
  1074. int ret;
  1075. if(sub->start_display_time) {
  1076. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1077. return -1;
  1078. }
  1079. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  1080. avctx->frame_number++;
  1081. return ret;
  1082. }
  1083. /**
  1084. * Attempt to guess proper monotonic timestamps for decoded video frames
  1085. * which might have incorrect times. Input timestamps may wrap around, in
  1086. * which case the output will as well.
  1087. *
  1088. * @param pts the pts field of the decoded AVPacket, as passed through
  1089. * AVFrame.pkt_pts
  1090. * @param dts the dts field of the decoded AVPacket
  1091. * @return one of the input values, may be AV_NOPTS_VALUE
  1092. */
  1093. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1094. int64_t reordered_pts, int64_t dts)
  1095. {
  1096. int64_t pts = AV_NOPTS_VALUE;
  1097. if (dts != AV_NOPTS_VALUE) {
  1098. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1099. ctx->pts_correction_last_dts = dts;
  1100. }
  1101. if (reordered_pts != AV_NOPTS_VALUE) {
  1102. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1103. ctx->pts_correction_last_pts = reordered_pts;
  1104. }
  1105. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1106. && reordered_pts != AV_NOPTS_VALUE)
  1107. pts = reordered_pts;
  1108. else
  1109. pts = dts;
  1110. return pts;
  1111. }
  1112. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1113. {
  1114. int size = 0;
  1115. const uint8_t *data;
  1116. uint32_t flags;
  1117. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1118. return;
  1119. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1120. if (!data || size < 4)
  1121. return;
  1122. flags = bytestream_get_le32(&data);
  1123. size -= 4;
  1124. if (size < 4) /* Required for any of the changes */
  1125. return;
  1126. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1127. avctx->channels = bytestream_get_le32(&data);
  1128. size -= 4;
  1129. }
  1130. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1131. if (size < 8)
  1132. return;
  1133. avctx->channel_layout = bytestream_get_le64(&data);
  1134. size -= 8;
  1135. }
  1136. if (size < 4)
  1137. return;
  1138. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1139. avctx->sample_rate = bytestream_get_le32(&data);
  1140. size -= 4;
  1141. }
  1142. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1143. if (size < 8)
  1144. return;
  1145. avctx->width = bytestream_get_le32(&data);
  1146. avctx->height = bytestream_get_le32(&data);
  1147. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1148. size -= 8;
  1149. }
  1150. }
  1151. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1152. int *got_picture_ptr,
  1153. const AVPacket *avpkt)
  1154. {
  1155. int ret;
  1156. // copy to ensure we do not change avpkt
  1157. AVPacket tmp = *avpkt;
  1158. *got_picture_ptr= 0;
  1159. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1160. return -1;
  1161. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1162. int did_split = av_packet_split_side_data(&tmp);
  1163. apply_param_change(avctx, &tmp);
  1164. avctx->pkt = &tmp;
  1165. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1166. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1167. &tmp);
  1168. else {
  1169. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1170. &tmp);
  1171. picture->pkt_dts= avpkt->dts;
  1172. if(!avctx->has_b_frames){
  1173. picture->pkt_pos= avpkt->pos;
  1174. }
  1175. //FIXME these should be under if(!avctx->has_b_frames)
  1176. if (!picture->sample_aspect_ratio.num)
  1177. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1178. if (!picture->width)
  1179. picture->width = avctx->width;
  1180. if (!picture->height)
  1181. picture->height = avctx->height;
  1182. if (picture->format == PIX_FMT_NONE)
  1183. picture->format = avctx->pix_fmt;
  1184. }
  1185. emms_c(); //needed to avoid an emms_c() call before every return;
  1186. avctx->pkt = NULL;
  1187. if (did_split)
  1188. ff_packet_free_side_data(&tmp);
  1189. if (*got_picture_ptr){
  1190. avctx->frame_number++;
  1191. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1192. picture->pkt_pts,
  1193. picture->pkt_dts);
  1194. }
  1195. }else
  1196. ret= 0;
  1197. return ret;
  1198. }
  1199. #if FF_API_OLD_DECODE_AUDIO
  1200. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1201. int *frame_size_ptr,
  1202. AVPacket *avpkt)
  1203. {
  1204. AVFrame frame;
  1205. int ret, got_frame = 0;
  1206. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1207. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1208. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1209. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1210. "avcodec_decode_audio4()\n");
  1211. avctx->get_buffer = avcodec_default_get_buffer;
  1212. avctx->release_buffer = avcodec_default_release_buffer;
  1213. }
  1214. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1215. if (ret >= 0 && got_frame) {
  1216. int ch, plane_size;
  1217. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1218. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1219. frame.nb_samples,
  1220. avctx->sample_fmt, 1);
  1221. if (*frame_size_ptr < data_size) {
  1222. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1223. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1224. return AVERROR(EINVAL);
  1225. }
  1226. memcpy(samples, frame.extended_data[0], plane_size);
  1227. if (planar && avctx->channels > 1) {
  1228. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1229. for (ch = 1; ch < avctx->channels; ch++) {
  1230. memcpy(out, frame.extended_data[ch], plane_size);
  1231. out += plane_size;
  1232. }
  1233. }
  1234. *frame_size_ptr = data_size;
  1235. } else {
  1236. *frame_size_ptr = 0;
  1237. }
  1238. return ret;
  1239. }
  1240. #endif
  1241. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1242. AVFrame *frame,
  1243. int *got_frame_ptr,
  1244. AVPacket *avpkt)
  1245. {
  1246. int ret = 0;
  1247. *got_frame_ptr = 0;
  1248. if (!avpkt->data && avpkt->size) {
  1249. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1250. return AVERROR(EINVAL);
  1251. }
  1252. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1253. av_packet_split_side_data(avpkt);
  1254. apply_param_change(avctx, avpkt);
  1255. avctx->pkt = avpkt;
  1256. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1257. if (ret >= 0 && *got_frame_ptr) {
  1258. avctx->frame_number++;
  1259. frame->pkt_dts = avpkt->dts;
  1260. if (frame->format == AV_SAMPLE_FMT_NONE)
  1261. frame->format = avctx->sample_fmt;
  1262. }
  1263. }
  1264. return ret;
  1265. }
  1266. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1267. int *got_sub_ptr,
  1268. AVPacket *avpkt)
  1269. {
  1270. int ret;
  1271. avctx->pkt = avpkt;
  1272. *got_sub_ptr = 0;
  1273. avcodec_get_subtitle_defaults(sub);
  1274. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1275. if (*got_sub_ptr)
  1276. avctx->frame_number++;
  1277. return ret;
  1278. }
  1279. void avsubtitle_free(AVSubtitle *sub)
  1280. {
  1281. int i;
  1282. for (i = 0; i < sub->num_rects; i++)
  1283. {
  1284. av_freep(&sub->rects[i]->pict.data[0]);
  1285. av_freep(&sub->rects[i]->pict.data[1]);
  1286. av_freep(&sub->rects[i]->pict.data[2]);
  1287. av_freep(&sub->rects[i]->pict.data[3]);
  1288. av_freep(&sub->rects[i]->text);
  1289. av_freep(&sub->rects[i]->ass);
  1290. av_freep(&sub->rects[i]);
  1291. }
  1292. av_freep(&sub->rects);
  1293. memset(sub, 0, sizeof(AVSubtitle));
  1294. }
  1295. av_cold int avcodec_close(AVCodecContext *avctx)
  1296. {
  1297. /* If there is a user-supplied mutex locking routine, call it. */
  1298. if (ff_lockmgr_cb) {
  1299. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1300. return -1;
  1301. }
  1302. entangled_thread_counter++;
  1303. if(entangled_thread_counter != 1){
  1304. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1305. entangled_thread_counter--;
  1306. return -1;
  1307. }
  1308. if (avcodec_is_open(avctx)) {
  1309. if (HAVE_THREADS && avctx->thread_opaque)
  1310. ff_thread_free(avctx);
  1311. if (avctx->codec && avctx->codec->close)
  1312. avctx->codec->close(avctx);
  1313. avcodec_default_free_buffers(avctx);
  1314. avctx->coded_frame = NULL;
  1315. av_freep(&avctx->internal);
  1316. }
  1317. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1318. av_opt_free(avctx->priv_data);
  1319. av_opt_free(avctx);
  1320. av_freep(&avctx->priv_data);
  1321. if (codec_is_encoder(avctx->codec))
  1322. av_freep(&avctx->extradata);
  1323. avctx->codec = NULL;
  1324. avctx->active_thread_type = 0;
  1325. entangled_thread_counter--;
  1326. /* Release any user-supplied mutex. */
  1327. if (ff_lockmgr_cb) {
  1328. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1329. }
  1330. return 0;
  1331. }
  1332. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1333. {
  1334. switch(id){
  1335. //This is for future deprecatec codec ids, its empty since
  1336. //last major bump but will fill up again over time, please dont remove it
  1337. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1338. default : return id;
  1339. }
  1340. }
  1341. AVCodec *avcodec_find_encoder(enum CodecID id)
  1342. {
  1343. AVCodec *p, *experimental=NULL;
  1344. p = first_avcodec;
  1345. id= remap_deprecated_codec_id(id);
  1346. while (p) {
  1347. if (codec_is_encoder(p) && p->id == id) {
  1348. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1349. experimental = p;
  1350. } else
  1351. return p;
  1352. }
  1353. p = p->next;
  1354. }
  1355. return experimental;
  1356. }
  1357. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1358. {
  1359. AVCodec *p;
  1360. if (!name)
  1361. return NULL;
  1362. p = first_avcodec;
  1363. while (p) {
  1364. if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1365. return p;
  1366. p = p->next;
  1367. }
  1368. return NULL;
  1369. }
  1370. AVCodec *avcodec_find_decoder(enum CodecID id)
  1371. {
  1372. AVCodec *p, *experimental=NULL;
  1373. p = first_avcodec;
  1374. id= remap_deprecated_codec_id(id);
  1375. while (p) {
  1376. if (codec_is_decoder(p) && p->id == id) {
  1377. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1378. experimental = p;
  1379. } else
  1380. return p;
  1381. }
  1382. p = p->next;
  1383. }
  1384. return experimental;
  1385. }
  1386. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1387. {
  1388. AVCodec *p;
  1389. if (!name)
  1390. return NULL;
  1391. p = first_avcodec;
  1392. while (p) {
  1393. if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1394. return p;
  1395. p = p->next;
  1396. }
  1397. return NULL;
  1398. }
  1399. static int get_bit_rate(AVCodecContext *ctx)
  1400. {
  1401. int bit_rate;
  1402. int bits_per_sample;
  1403. switch(ctx->codec_type) {
  1404. case AVMEDIA_TYPE_VIDEO:
  1405. case AVMEDIA_TYPE_DATA:
  1406. case AVMEDIA_TYPE_SUBTITLE:
  1407. case AVMEDIA_TYPE_ATTACHMENT:
  1408. bit_rate = ctx->bit_rate;
  1409. break;
  1410. case AVMEDIA_TYPE_AUDIO:
  1411. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1412. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1413. break;
  1414. default:
  1415. bit_rate = 0;
  1416. break;
  1417. }
  1418. return bit_rate;
  1419. }
  1420. const char *avcodec_get_name(enum CodecID id)
  1421. {
  1422. AVCodec *codec;
  1423. #if !CONFIG_SMALL
  1424. switch (id) {
  1425. #include "libavcodec/codec_names.h"
  1426. }
  1427. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1428. #endif
  1429. codec = avcodec_find_decoder(id);
  1430. if (codec)
  1431. return codec->name;
  1432. codec = avcodec_find_encoder(id);
  1433. if (codec)
  1434. return codec->name;
  1435. return "unknown_codec";
  1436. }
  1437. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1438. {
  1439. int i, len, ret = 0;
  1440. for (i = 0; i < 4; i++) {
  1441. len = snprintf(buf, buf_size,
  1442. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1443. buf += len;
  1444. buf_size = buf_size > len ? buf_size - len : 0;
  1445. ret += len;
  1446. codec_tag>>=8;
  1447. }
  1448. return ret;
  1449. }
  1450. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1451. {
  1452. const char *codec_type;
  1453. const char *codec_name;
  1454. const char *profile = NULL;
  1455. AVCodec *p;
  1456. int bitrate;
  1457. AVRational display_aspect_ratio;
  1458. if (!buf || buf_size <= 0)
  1459. return;
  1460. codec_type = av_get_media_type_string(enc->codec_type);
  1461. codec_name = avcodec_get_name(enc->codec_id);
  1462. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1463. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1464. avcodec_find_decoder(enc->codec_id);
  1465. if (p)
  1466. profile = av_get_profile_name(p, enc->profile);
  1467. }
  1468. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1469. codec_name, enc->mb_decision ? " (hq)" : "");
  1470. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1471. if (profile)
  1472. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1473. if (enc->codec_tag) {
  1474. char tag_buf[32];
  1475. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1476. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1477. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1478. }
  1479. switch(enc->codec_type) {
  1480. case AVMEDIA_TYPE_VIDEO:
  1481. if (enc->pix_fmt != PIX_FMT_NONE) {
  1482. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1483. ", %s",
  1484. av_get_pix_fmt_name(enc->pix_fmt));
  1485. }
  1486. if (enc->width) {
  1487. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1488. ", %dx%d",
  1489. enc->width, enc->height);
  1490. if (enc->sample_aspect_ratio.num) {
  1491. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1492. enc->width*enc->sample_aspect_ratio.num,
  1493. enc->height*enc->sample_aspect_ratio.den,
  1494. 1024*1024);
  1495. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1496. " [SAR %d:%d DAR %d:%d]",
  1497. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1498. display_aspect_ratio.num, display_aspect_ratio.den);
  1499. }
  1500. if(av_log_get_level() >= AV_LOG_DEBUG){
  1501. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1502. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1503. ", %d/%d",
  1504. enc->time_base.num/g, enc->time_base.den/g);
  1505. }
  1506. }
  1507. if (encode) {
  1508. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1509. ", q=%d-%d", enc->qmin, enc->qmax);
  1510. }
  1511. break;
  1512. case AVMEDIA_TYPE_AUDIO:
  1513. if (enc->sample_rate) {
  1514. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1515. ", %d Hz", enc->sample_rate);
  1516. }
  1517. av_strlcat(buf, ", ", buf_size);
  1518. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1519. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1520. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1521. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1522. }
  1523. break;
  1524. default:
  1525. return;
  1526. }
  1527. if (encode) {
  1528. if (enc->flags & CODEC_FLAG_PASS1)
  1529. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1530. ", pass 1");
  1531. if (enc->flags & CODEC_FLAG_PASS2)
  1532. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1533. ", pass 2");
  1534. }
  1535. bitrate = get_bit_rate(enc);
  1536. if (bitrate != 0) {
  1537. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1538. ", %d kb/s", bitrate / 1000);
  1539. }
  1540. }
  1541. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1542. {
  1543. const AVProfile *p;
  1544. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1545. return NULL;
  1546. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1547. if (p->profile == profile)
  1548. return p->name;
  1549. return NULL;
  1550. }
  1551. unsigned avcodec_version( void )
  1552. {
  1553. // av_assert0(CODEC_ID_V410==164);
  1554. av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
  1555. av_assert0(CODEC_ID_ADPCM_G722==69660);
  1556. // av_assert0(CODEC_ID_BMV_AUDIO==86071);
  1557. av_assert0(CODEC_ID_SRT==94216);
  1558. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1559. return LIBAVCODEC_VERSION_INT;
  1560. }
  1561. const char *avcodec_configuration(void)
  1562. {
  1563. return FFMPEG_CONFIGURATION;
  1564. }
  1565. const char *avcodec_license(void)
  1566. {
  1567. #define LICENSE_PREFIX "libavcodec license: "
  1568. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1569. }
  1570. void avcodec_flush_buffers(AVCodecContext *avctx)
  1571. {
  1572. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1573. ff_thread_flush(avctx);
  1574. else if(avctx->codec->flush)
  1575. avctx->codec->flush(avctx);
  1576. }
  1577. static void video_free_buffers(AVCodecContext *s)
  1578. {
  1579. AVCodecInternal *avci = s->internal;
  1580. int i, j;
  1581. if (!avci->buffer)
  1582. return;
  1583. if (avci->buffer_count)
  1584. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1585. avci->buffer_count);
  1586. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1587. InternalBuffer *buf = &avci->buffer[i];
  1588. for(j=0; j<4; j++){
  1589. av_freep(&buf->base[j]);
  1590. buf->data[j]= NULL;
  1591. }
  1592. }
  1593. av_freep(&avci->buffer);
  1594. avci->buffer_count=0;
  1595. }
  1596. static void audio_free_buffers(AVCodecContext *avctx)
  1597. {
  1598. AVCodecInternal *avci = avctx->internal;
  1599. InternalBuffer *buf;
  1600. if (!avci->buffer)
  1601. return;
  1602. buf = avci->buffer;
  1603. if (buf->extended_data) {
  1604. av_free(buf->extended_data[0]);
  1605. if (buf->extended_data != buf->data)
  1606. av_freep(&buf->extended_data);
  1607. }
  1608. av_freep(&avci->buffer);
  1609. }
  1610. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1611. {
  1612. switch (avctx->codec_type) {
  1613. case AVMEDIA_TYPE_VIDEO:
  1614. video_free_buffers(avctx);
  1615. break;
  1616. case AVMEDIA_TYPE_AUDIO:
  1617. audio_free_buffers(avctx);
  1618. break;
  1619. default:
  1620. break;
  1621. }
  1622. }
  1623. int av_get_bits_per_sample(enum CodecID codec_id){
  1624. switch(codec_id){
  1625. case CODEC_ID_ADPCM_SBPRO_2:
  1626. return 2;
  1627. case CODEC_ID_ADPCM_SBPRO_3:
  1628. return 3;
  1629. case CODEC_ID_ADPCM_SBPRO_4:
  1630. case CODEC_ID_ADPCM_CT:
  1631. case CODEC_ID_ADPCM_IMA_APC:
  1632. case CODEC_ID_ADPCM_IMA_WAV:
  1633. case CODEC_ID_ADPCM_IMA_QT:
  1634. case CODEC_ID_ADPCM_SWF:
  1635. case CODEC_ID_ADPCM_MS:
  1636. case CODEC_ID_ADPCM_YAMAHA:
  1637. case CODEC_ID_ADPCM_G722:
  1638. return 4;
  1639. case CODEC_ID_PCM_ALAW:
  1640. case CODEC_ID_PCM_MULAW:
  1641. case CODEC_ID_PCM_S8:
  1642. case CODEC_ID_PCM_U8:
  1643. case CODEC_ID_PCM_ZORK:
  1644. return 8;
  1645. case CODEC_ID_PCM_S16BE:
  1646. case CODEC_ID_PCM_S16LE:
  1647. case CODEC_ID_PCM_S16LE_PLANAR:
  1648. case CODEC_ID_PCM_U16BE:
  1649. case CODEC_ID_PCM_U16LE:
  1650. return 16;
  1651. case CODEC_ID_PCM_S24DAUD:
  1652. case CODEC_ID_PCM_S24BE:
  1653. case CODEC_ID_PCM_S24LE:
  1654. case CODEC_ID_PCM_U24BE:
  1655. case CODEC_ID_PCM_U24LE:
  1656. return 24;
  1657. case CODEC_ID_PCM_S32BE:
  1658. case CODEC_ID_PCM_S32LE:
  1659. case CODEC_ID_PCM_U32BE:
  1660. case CODEC_ID_PCM_U32LE:
  1661. case CODEC_ID_PCM_F32BE:
  1662. case CODEC_ID_PCM_F32LE:
  1663. return 32;
  1664. case CODEC_ID_PCM_F64BE:
  1665. case CODEC_ID_PCM_F64LE:
  1666. return 64;
  1667. default:
  1668. return 0;
  1669. }
  1670. }
  1671. #if !HAVE_THREADS
  1672. int ff_thread_init(AVCodecContext *s){
  1673. return -1;
  1674. }
  1675. #endif
  1676. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1677. {
  1678. unsigned int n = 0;
  1679. while(v >= 0xff) {
  1680. *s++ = 0xff;
  1681. v -= 0xff;
  1682. n++;
  1683. }
  1684. *s = v;
  1685. n++;
  1686. return n;
  1687. }
  1688. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1689. int i;
  1690. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1691. return i;
  1692. }
  1693. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1694. {
  1695. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1696. "version to the newest one from Git. If the problem still "
  1697. "occurs, it means that your file has a feature which has not "
  1698. "been implemented.\n", feature);
  1699. if(want_sample)
  1700. av_log_ask_for_sample(avc, NULL);
  1701. }
  1702. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1703. {
  1704. va_list argument_list;
  1705. va_start(argument_list, msg);
  1706. if (msg)
  1707. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1708. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1709. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1710. "and contact the ffmpeg-devel mailing list.\n");
  1711. va_end(argument_list);
  1712. }
  1713. static AVHWAccel *first_hwaccel = NULL;
  1714. void av_register_hwaccel(AVHWAccel *hwaccel)
  1715. {
  1716. AVHWAccel **p = &first_hwaccel;
  1717. while (*p)
  1718. p = &(*p)->next;
  1719. *p = hwaccel;
  1720. hwaccel->next = NULL;
  1721. }
  1722. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1723. {
  1724. return hwaccel ? hwaccel->next : first_hwaccel;
  1725. }
  1726. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1727. {
  1728. AVHWAccel *hwaccel=NULL;
  1729. while((hwaccel= av_hwaccel_next(hwaccel))){
  1730. if ( hwaccel->id == codec_id
  1731. && hwaccel->pix_fmt == pix_fmt)
  1732. return hwaccel;
  1733. }
  1734. return NULL;
  1735. }
  1736. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1737. {
  1738. if (ff_lockmgr_cb) {
  1739. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1740. return -1;
  1741. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1742. return -1;
  1743. }
  1744. ff_lockmgr_cb = cb;
  1745. if (ff_lockmgr_cb) {
  1746. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1747. return -1;
  1748. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1749. return -1;
  1750. }
  1751. return 0;
  1752. }
  1753. int avpriv_lock_avformat(void)
  1754. {
  1755. if (ff_lockmgr_cb) {
  1756. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1757. return -1;
  1758. }
  1759. return 0;
  1760. }
  1761. int avpriv_unlock_avformat(void)
  1762. {
  1763. if (ff_lockmgr_cb) {
  1764. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1765. return -1;
  1766. }
  1767. return 0;
  1768. }
  1769. unsigned int avpriv_toupper4(unsigned int x)
  1770. {
  1771. return toupper( x &0xFF)
  1772. + (toupper((x>>8 )&0xFF)<<8 )
  1773. + (toupper((x>>16)&0xFF)<<16)
  1774. + (toupper((x>>24)&0xFF)<<24);
  1775. }
  1776. #if !HAVE_THREADS
  1777. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1778. {
  1779. f->owner = avctx;
  1780. ff_init_buffer_info(avctx, f);
  1781. return avctx->get_buffer(avctx, f);
  1782. }
  1783. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1784. {
  1785. f->owner->release_buffer(f->owner, f);
  1786. }
  1787. void ff_thread_finish_setup(AVCodecContext *avctx)
  1788. {
  1789. }
  1790. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1791. {
  1792. }
  1793. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1794. {
  1795. }
  1796. #endif
  1797. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1798. {
  1799. AVCodec *c= avcodec_find_decoder(codec_id);
  1800. if(!c)
  1801. c= avcodec_find_encoder(codec_id);
  1802. if(c)
  1803. return c->type;
  1804. if (codec_id <= CODEC_ID_NONE)
  1805. return AVMEDIA_TYPE_UNKNOWN;
  1806. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1807. return AVMEDIA_TYPE_VIDEO;
  1808. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1809. return AVMEDIA_TYPE_AUDIO;
  1810. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1811. return AVMEDIA_TYPE_SUBTITLE;
  1812. return AVMEDIA_TYPE_UNKNOWN;
  1813. }
  1814. int avcodec_is_open(AVCodecContext *s)
  1815. {
  1816. return !!s->internal;
  1817. }