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.

2082 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. ff_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_YUVA444P:
  157. case PIX_FMT_YUV420P9LE:
  158. case PIX_FMT_YUV420P9BE:
  159. case PIX_FMT_YUV420P10LE:
  160. case PIX_FMT_YUV420P10BE:
  161. case PIX_FMT_YUV422P9LE:
  162. case PIX_FMT_YUV422P9BE:
  163. case PIX_FMT_YUV422P10LE:
  164. case PIX_FMT_YUV422P10BE:
  165. case PIX_FMT_YUV444P9LE:
  166. case PIX_FMT_YUV444P9BE:
  167. case PIX_FMT_YUV444P10LE:
  168. case PIX_FMT_YUV444P10BE:
  169. case PIX_FMT_GBRP9LE:
  170. case PIX_FMT_GBRP9BE:
  171. case PIX_FMT_GBRP10LE:
  172. case PIX_FMT_GBRP10BE:
  173. w_align = 16; //FIXME assume 16 pixel per macroblock
  174. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  175. break;
  176. case PIX_FMT_YUV411P:
  177. case PIX_FMT_UYYVYY411:
  178. w_align=32;
  179. h_align=8;
  180. break;
  181. case PIX_FMT_YUV410P:
  182. if(s->codec_id == CODEC_ID_SVQ1){
  183. w_align=64;
  184. h_align=64;
  185. }
  186. case PIX_FMT_RGB555:
  187. if(s->codec_id == CODEC_ID_RPZA){
  188. w_align=4;
  189. h_align=4;
  190. }
  191. case PIX_FMT_PAL8:
  192. case PIX_FMT_BGR8:
  193. case PIX_FMT_RGB8:
  194. if(s->codec_id == CODEC_ID_SMC){
  195. w_align=4;
  196. h_align=4;
  197. }
  198. break;
  199. case PIX_FMT_BGR24:
  200. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  201. w_align=4;
  202. h_align=4;
  203. }
  204. break;
  205. default:
  206. w_align= 1;
  207. h_align= 1;
  208. break;
  209. }
  210. if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
  211. w_align= FFMAX(w_align, 8);
  212. }
  213. *width = FFALIGN(*width , w_align);
  214. *height= FFALIGN(*height, h_align);
  215. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  216. *height+=2; // some of the optimized chroma MC reads one line too much
  217. // which is also done in mpeg decoders with lowres > 0
  218. for (i = 0; i < 4; i++)
  219. linesize_align[i] = STRIDE_ALIGN;
  220. }
  221. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  222. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  223. int linesize_align[AV_NUM_DATA_POINTERS];
  224. int align;
  225. avcodec_align_dimensions2(s, width, height, linesize_align);
  226. align = FFMAX(linesize_align[0], linesize_align[3]);
  227. linesize_align[1] <<= chroma_shift;
  228. linesize_align[2] <<= chroma_shift;
  229. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  230. *width=FFALIGN(*width, align);
  231. }
  232. void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
  233. {
  234. if (s->pkt) {
  235. pic->pkt_pts = s->pkt->pts;
  236. pic->pkt_pos = s->pkt->pos;
  237. } else {
  238. pic->pkt_pts = AV_NOPTS_VALUE;
  239. pic->pkt_pos = -1;
  240. }
  241. pic->reordered_opaque= s->reordered_opaque;
  242. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  243. pic->width = s->width;
  244. pic->height = s->height;
  245. pic->format = s->pix_fmt;
  246. }
  247. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  248. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  249. int buf_size, int align)
  250. {
  251. int ch, planar, needed_size, ret = 0;
  252. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  253. frame->nb_samples, sample_fmt,
  254. align);
  255. if (buf_size < needed_size)
  256. return AVERROR(EINVAL);
  257. planar = av_sample_fmt_is_planar(sample_fmt);
  258. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  259. if (!(frame->extended_data = av_mallocz(nb_channels *
  260. sizeof(*frame->extended_data))))
  261. return AVERROR(ENOMEM);
  262. } else {
  263. frame->extended_data = frame->data;
  264. }
  265. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  266. buf, nb_channels, frame->nb_samples,
  267. sample_fmt, align)) < 0) {
  268. if (frame->extended_data != frame->data)
  269. av_freep(&frame->extended_data);
  270. return ret;
  271. }
  272. if (frame->extended_data != frame->data) {
  273. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  274. frame->data[ch] = frame->extended_data[ch];
  275. }
  276. return ret;
  277. }
  278. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  279. {
  280. AVCodecInternal *avci = avctx->internal;
  281. InternalBuffer *buf;
  282. int buf_size, ret;
  283. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  284. frame->nb_samples, avctx->sample_fmt,
  285. 32);
  286. if (buf_size < 0)
  287. return AVERROR(EINVAL);
  288. /* allocate InternalBuffer if needed */
  289. if (!avci->buffer) {
  290. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  291. if (!avci->buffer)
  292. return AVERROR(ENOMEM);
  293. }
  294. buf = avci->buffer;
  295. /* if there is a previously-used internal buffer, check its size and
  296. channel count to see if we can reuse it */
  297. if (buf->extended_data) {
  298. /* if current buffer is too small, free it */
  299. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  300. av_free(buf->extended_data[0]);
  301. if (buf->extended_data != buf->data)
  302. av_freep(&buf->extended_data);
  303. buf->extended_data = NULL;
  304. buf->data[0] = NULL;
  305. }
  306. /* if number of channels has changed, reset and/or free extended data
  307. pointers but leave data buffer in buf->data[0] for reuse */
  308. if (buf->nb_channels != avctx->channels) {
  309. if (buf->extended_data != buf->data)
  310. av_free(buf->extended_data);
  311. buf->extended_data = NULL;
  312. }
  313. }
  314. /* if there is no previous buffer or the previous buffer cannot be used
  315. as-is, allocate a new buffer and/or rearrange the channel pointers */
  316. if (!buf->extended_data) {
  317. if (!buf->data[0]) {
  318. if (!(buf->data[0] = av_mallocz(buf_size)))
  319. return AVERROR(ENOMEM);
  320. buf->audio_data_size = buf_size;
  321. }
  322. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  323. avctx->sample_fmt, buf->data[0],
  324. buf->audio_data_size, 32)))
  325. return ret;
  326. if (frame->extended_data == frame->data)
  327. buf->extended_data = buf->data;
  328. else
  329. buf->extended_data = frame->extended_data;
  330. memcpy(buf->data, frame->data, sizeof(frame->data));
  331. buf->linesize[0] = frame->linesize[0];
  332. buf->nb_channels = avctx->channels;
  333. } else {
  334. /* copy InternalBuffer info to the AVFrame */
  335. frame->extended_data = buf->extended_data;
  336. frame->linesize[0] = buf->linesize[0];
  337. memcpy(frame->data, buf->data, sizeof(frame->data));
  338. }
  339. frame->type = FF_BUFFER_TYPE_INTERNAL;
  340. if (avctx->pkt) {
  341. frame->pkt_pts = avctx->pkt->pts;
  342. frame->pkt_pos = avctx->pkt->pos;
  343. } else {
  344. frame->pkt_pts = AV_NOPTS_VALUE;
  345. frame->pkt_pos = -1;
  346. }
  347. frame->reordered_opaque = avctx->reordered_opaque;
  348. if (avctx->debug & FF_DEBUG_BUFFERS)
  349. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  350. "internal audio buffer used\n", frame);
  351. return 0;
  352. }
  353. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  354. {
  355. int i;
  356. int w= s->width;
  357. int h= s->height;
  358. InternalBuffer *buf;
  359. AVCodecInternal *avci = s->internal;
  360. if(pic->data[0]!=NULL) {
  361. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  362. return -1;
  363. }
  364. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  365. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  366. return -1;
  367. }
  368. if(av_image_check_size(w, h, 0, s))
  369. return -1;
  370. if (!avci->buffer) {
  371. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  372. sizeof(InternalBuffer));
  373. }
  374. buf = &avci->buffer[avci->buffer_count];
  375. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  376. if(s->active_thread_type&FF_THREAD_FRAME) {
  377. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  378. return -1;
  379. }
  380. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  381. av_freep(&buf->base[i]);
  382. buf->data[i]= NULL;
  383. }
  384. }
  385. if (!buf->base[0]) {
  386. int h_chroma_shift, v_chroma_shift;
  387. int size[4] = {0};
  388. int tmpsize;
  389. int unaligned;
  390. AVPicture picture;
  391. int stride_align[AV_NUM_DATA_POINTERS];
  392. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  393. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  394. avcodec_align_dimensions2(s, &w, &h, stride_align);
  395. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  396. w+= EDGE_WIDTH*2;
  397. h+= EDGE_WIDTH*2;
  398. }
  399. do {
  400. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  401. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  402. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  403. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  404. w += w & ~(w-1);
  405. unaligned = 0;
  406. for (i=0; i<4; i++){
  407. unaligned |= picture.linesize[i] % stride_align[i];
  408. }
  409. } while (unaligned);
  410. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  411. if (tmpsize < 0)
  412. return -1;
  413. for (i=0; i<3 && picture.data[i+1]; i++)
  414. size[i] = picture.data[i+1] - picture.data[i];
  415. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  416. memset(buf->base, 0, sizeof(buf->base));
  417. memset(buf->data, 0, sizeof(buf->data));
  418. for(i=0; i<4 && size[i]; i++){
  419. const int h_shift= i==0 ? 0 : h_chroma_shift;
  420. const int v_shift= i==0 ? 0 : v_chroma_shift;
  421. buf->linesize[i]= picture.linesize[i];
  422. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  423. if(buf->base[i]==NULL) return -1;
  424. memset(buf->base[i], 128, size[i]);
  425. // no edge if EDGE EMU or not planar YUV
  426. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  427. buf->data[i] = buf->base[i];
  428. else
  429. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  430. }
  431. for (; i < AV_NUM_DATA_POINTERS; i++) {
  432. buf->base[i] = buf->data[i] = NULL;
  433. buf->linesize[i] = 0;
  434. }
  435. if(size[1] && !size[2])
  436. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  437. buf->width = s->width;
  438. buf->height = s->height;
  439. buf->pix_fmt= s->pix_fmt;
  440. }
  441. pic->type= FF_BUFFER_TYPE_INTERNAL;
  442. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  443. pic->base[i]= buf->base[i];
  444. pic->data[i]= buf->data[i];
  445. pic->linesize[i]= buf->linesize[i];
  446. }
  447. pic->extended_data = pic->data;
  448. avci->buffer_count++;
  449. if (s->pkt) {
  450. pic->pkt_pts = s->pkt->pts;
  451. pic->pkt_pos = s->pkt->pos;
  452. } else {
  453. pic->pkt_pts = AV_NOPTS_VALUE;
  454. pic->pkt_pos = -1;
  455. }
  456. pic->reordered_opaque= s->reordered_opaque;
  457. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  458. pic->width = s->width;
  459. pic->height = s->height;
  460. pic->format = s->pix_fmt;
  461. if(s->debug&FF_DEBUG_BUFFERS)
  462. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  463. "buffers used\n", pic, avci->buffer_count);
  464. return 0;
  465. }
  466. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  467. {
  468. switch (avctx->codec_type) {
  469. case AVMEDIA_TYPE_VIDEO:
  470. return video_get_buffer(avctx, frame);
  471. case AVMEDIA_TYPE_AUDIO:
  472. return audio_get_buffer(avctx, frame);
  473. default:
  474. return -1;
  475. }
  476. }
  477. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  478. int i;
  479. InternalBuffer *buf, *last;
  480. AVCodecInternal *avci = s->internal;
  481. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  482. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  483. assert(avci->buffer_count);
  484. if (avci->buffer) {
  485. buf = NULL; /* avoids warning */
  486. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  487. buf = &avci->buffer[i];
  488. if (buf->data[0] == pic->data[0])
  489. break;
  490. }
  491. assert(i < avci->buffer_count);
  492. avci->buffer_count--;
  493. last = &avci->buffer[avci->buffer_count];
  494. if (buf != last)
  495. FFSWAP(InternalBuffer, *buf, *last);
  496. }
  497. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  498. pic->data[i]=NULL;
  499. // pic->base[i]=NULL;
  500. }
  501. //printf("R%X\n", pic->opaque);
  502. if(s->debug&FF_DEBUG_BUFFERS)
  503. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  504. "buffers used\n", pic, avci->buffer_count);
  505. }
  506. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  507. AVFrame temp_pic;
  508. int i;
  509. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  510. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  511. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  512. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  513. s->release_buffer(s, pic);
  514. }
  515. ff_init_buffer_info(s, pic);
  516. /* If no picture return a new buffer */
  517. if(pic->data[0] == NULL) {
  518. /* We will copy from buffer, so must be readable */
  519. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  520. return s->get_buffer(s, pic);
  521. }
  522. /* If internal buffer type return the same buffer */
  523. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  524. return 0;
  525. }
  526. /*
  527. * Not internal type and reget_buffer not overridden, emulate cr buffer
  528. */
  529. temp_pic = *pic;
  530. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  531. pic->data[i] = pic->base[i] = NULL;
  532. pic->opaque = NULL;
  533. /* Allocate new frame */
  534. if (s->get_buffer(s, pic))
  535. return -1;
  536. /* Copy image data from old buffer to new buffer */
  537. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  538. s->height);
  539. s->release_buffer(s, &temp_pic); // Release old frame
  540. return 0;
  541. }
  542. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  543. int i;
  544. for(i=0; i<count; i++){
  545. int r= func(c, (char*)arg + i*size);
  546. if(ret) ret[i]= r;
  547. }
  548. return 0;
  549. }
  550. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  551. int i;
  552. for(i=0; i<count; i++){
  553. int r= func(c, arg, i, 0);
  554. if(ret) ret[i]= r;
  555. }
  556. return 0;
  557. }
  558. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  559. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  560. ++fmt;
  561. return fmt[0];
  562. }
  563. void avcodec_get_frame_defaults(AVFrame *pic){
  564. memset(pic, 0, sizeof(AVFrame));
  565. pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  566. pic->pkt_pos = -1;
  567. pic->key_frame= 1;
  568. pic->sample_aspect_ratio = (AVRational){0, 1};
  569. pic->format = -1; /* unknown */
  570. }
  571. AVFrame *avcodec_alloc_frame(void){
  572. AVFrame *pic= av_malloc(sizeof(AVFrame));
  573. if(pic==NULL) return NULL;
  574. avcodec_get_frame_defaults(pic);
  575. return pic;
  576. }
  577. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  578. {
  579. memset(sub, 0, sizeof(*sub));
  580. sub->pts = AV_NOPTS_VALUE;
  581. }
  582. static int get_bit_rate(AVCodecContext *ctx)
  583. {
  584. int bit_rate;
  585. int bits_per_sample;
  586. switch(ctx->codec_type) {
  587. case AVMEDIA_TYPE_VIDEO:
  588. case AVMEDIA_TYPE_DATA:
  589. case AVMEDIA_TYPE_SUBTITLE:
  590. case AVMEDIA_TYPE_ATTACHMENT:
  591. bit_rate = ctx->bit_rate;
  592. break;
  593. case AVMEDIA_TYPE_AUDIO:
  594. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  595. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  596. break;
  597. default:
  598. bit_rate = 0;
  599. break;
  600. }
  601. return bit_rate;
  602. }
  603. #if FF_API_AVCODEC_OPEN
  604. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  605. {
  606. return avcodec_open2(avctx, codec, NULL);
  607. }
  608. #endif
  609. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  610. {
  611. int ret = 0;
  612. AVDictionary *tmp = NULL;
  613. if (avcodec_is_open(avctx))
  614. return 0;
  615. if ((!codec && !avctx->codec)) {
  616. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  617. return AVERROR(EINVAL);
  618. }
  619. if ((codec && avctx->codec && codec != avctx->codec)) {
  620. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  621. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  622. return AVERROR(EINVAL);
  623. }
  624. if (!codec)
  625. codec = avctx->codec;
  626. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  627. return AVERROR(EINVAL);
  628. if (options)
  629. av_dict_copy(&tmp, *options, 0);
  630. /* If there is a user-supplied mutex locking routine, call it. */
  631. if (ff_lockmgr_cb) {
  632. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  633. return -1;
  634. }
  635. entangled_thread_counter++;
  636. if(entangled_thread_counter != 1){
  637. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  638. ret = -1;
  639. goto end;
  640. }
  641. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  642. if (!avctx->internal) {
  643. ret = AVERROR(ENOMEM);
  644. goto end;
  645. }
  646. if (codec->priv_data_size > 0) {
  647. if(!avctx->priv_data){
  648. avctx->priv_data = av_mallocz(codec->priv_data_size);
  649. if (!avctx->priv_data) {
  650. ret = AVERROR(ENOMEM);
  651. goto end;
  652. }
  653. if (codec->priv_class) {
  654. *(AVClass**)avctx->priv_data= codec->priv_class;
  655. av_opt_set_defaults(avctx->priv_data);
  656. }
  657. }
  658. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  659. goto free_and_end;
  660. } else {
  661. avctx->priv_data = NULL;
  662. }
  663. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  664. goto free_and_end;
  665. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  666. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  667. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
  668. ret = -1;
  669. goto free_and_end;
  670. }
  671. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  672. if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
  673. if(avctx->coded_width && avctx->coded_height)
  674. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  675. else if(avctx->width && avctx->height)
  676. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  677. }
  678. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  679. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  680. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  681. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  682. avcodec_set_dimensions(avctx, 0, 0);
  683. }
  684. /* if the decoder init function was already called previously,
  685. free the already allocated subtitle_header before overwriting it */
  686. if (codec_is_decoder(codec))
  687. av_freep(&avctx->subtitle_header);
  688. #define SANE_NB_CHANNELS 128U
  689. if (avctx->channels > SANE_NB_CHANNELS) {
  690. ret = AVERROR(EINVAL);
  691. goto free_and_end;
  692. }
  693. avctx->codec = codec;
  694. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  695. avctx->codec_id == CODEC_ID_NONE) {
  696. avctx->codec_type = codec->type;
  697. avctx->codec_id = codec->id;
  698. }
  699. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  700. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  701. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  702. ret = AVERROR(EINVAL);
  703. goto free_and_end;
  704. }
  705. avctx->frame_number = 0;
  706. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  707. (!avctx->time_base.num || !avctx->time_base.den)) {
  708. avctx->time_base.num = 1;
  709. avctx->time_base.den = avctx->sample_rate;
  710. }
  711. if (!HAVE_THREADS)
  712. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  713. if (HAVE_THREADS && !avctx->thread_opaque) {
  714. ret = ff_thread_init(avctx);
  715. if (ret < 0) {
  716. goto free_and_end;
  717. }
  718. }
  719. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  720. avctx->thread_count = 1;
  721. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  722. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  723. avctx->codec->max_lowres);
  724. ret = AVERROR(EINVAL);
  725. goto free_and_end;
  726. }
  727. if (codec_is_encoder(avctx->codec)) {
  728. int i;
  729. if (avctx->codec->sample_fmts) {
  730. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  731. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  732. break;
  733. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  734. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  735. ret = AVERROR(EINVAL);
  736. goto free_and_end;
  737. }
  738. }
  739. if (avctx->codec->pix_fmts) {
  740. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  741. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  742. break;
  743. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
  744. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  745. ret = AVERROR(EINVAL);
  746. goto free_and_end;
  747. }
  748. }
  749. if (avctx->codec->supported_samplerates) {
  750. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  751. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  752. break;
  753. if (avctx->codec->supported_samplerates[i] == 0) {
  754. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  755. ret = AVERROR(EINVAL);
  756. goto free_and_end;
  757. }
  758. }
  759. if (avctx->codec->channel_layouts) {
  760. if (!avctx->channel_layout) {
  761. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  762. } else {
  763. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  764. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  765. break;
  766. if (avctx->codec->channel_layouts[i] == 0) {
  767. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  768. ret = AVERROR(EINVAL);
  769. goto free_and_end;
  770. }
  771. }
  772. }
  773. if (avctx->channel_layout && avctx->channels) {
  774. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  775. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  776. ret = AVERROR(EINVAL);
  777. goto free_and_end;
  778. }
  779. } else if (avctx->channel_layout) {
  780. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  781. }
  782. }
  783. avctx->pts_correction_num_faulty_pts =
  784. avctx->pts_correction_num_faulty_dts = 0;
  785. avctx->pts_correction_last_pts =
  786. avctx->pts_correction_last_dts = INT64_MIN;
  787. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  788. ret = avctx->codec->init(avctx);
  789. if (ret < 0) {
  790. goto free_and_end;
  791. }
  792. }
  793. if (codec_is_decoder(avctx->codec) && !avctx->bit_rate)
  794. avctx->bit_rate = get_bit_rate(avctx);
  795. ret=0;
  796. end:
  797. entangled_thread_counter--;
  798. /* Release any user-supplied mutex. */
  799. if (ff_lockmgr_cb) {
  800. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  801. }
  802. if (options) {
  803. av_dict_free(options);
  804. *options = tmp;
  805. }
  806. return ret;
  807. free_and_end:
  808. av_dict_free(&tmp);
  809. av_freep(&avctx->priv_data);
  810. av_freep(&avctx->internal);
  811. avctx->codec= NULL;
  812. goto end;
  813. }
  814. int ff_alloc_packet(AVPacket *avpkt, int size)
  815. {
  816. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  817. return AVERROR(EINVAL);
  818. if (avpkt->data) {
  819. uint8_t *pkt_data;
  820. if (avpkt->size < size)
  821. return AVERROR(EINVAL);
  822. pkt_data = avpkt->data;
  823. av_init_packet(avpkt);
  824. avpkt->data = pkt_data;
  825. avpkt->size = size;
  826. return 0;
  827. } else {
  828. return av_new_packet(avpkt, size);
  829. }
  830. }
  831. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  832. AVPacket *avpkt,
  833. const AVFrame *frame,
  834. int *got_packet_ptr)
  835. {
  836. int ret;
  837. int user_packet = !!avpkt->data;
  838. int nb_samples;
  839. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  840. av_init_packet(avpkt);
  841. avpkt->size = 0;
  842. return 0;
  843. }
  844. /* check for valid frame size */
  845. if (frame) {
  846. nb_samples = frame->nb_samples;
  847. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  848. if (nb_samples > avctx->frame_size)
  849. return AVERROR(EINVAL);
  850. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  851. if (nb_samples != avctx->frame_size)
  852. return AVERROR(EINVAL);
  853. }
  854. } else {
  855. nb_samples = avctx->frame_size;
  856. }
  857. if (avctx->codec->encode2) {
  858. *got_packet_ptr = 0;
  859. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  860. if (!ret && *got_packet_ptr) {
  861. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  862. avpkt->pts = frame->pts;
  863. avpkt->duration = ff_samples_to_time_base(avctx,
  864. frame->nb_samples);
  865. }
  866. avpkt->dts = avpkt->pts;
  867. } else {
  868. avpkt->size = 0;
  869. }
  870. } else {
  871. /* for compatibility with encoders not supporting encode2(), we need to
  872. allocate a packet buffer if the user has not provided one or check
  873. the size otherwise */
  874. int fs_tmp = 0;
  875. int buf_size = avpkt->size;
  876. if (!user_packet) {
  877. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  878. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  879. if (!frame)
  880. return AVERROR(EINVAL);
  881. buf_size = nb_samples * avctx->channels *
  882. av_get_bits_per_sample(avctx->codec_id) / 8;
  883. } else {
  884. /* this is a guess as to the required size.
  885. if an encoder needs more than this, it should probably
  886. implement encode2() */
  887. buf_size = 2 * avctx->frame_size * avctx->channels *
  888. av_get_bytes_per_sample(avctx->sample_fmt);
  889. buf_size += FF_MIN_BUFFER_SIZE;
  890. }
  891. }
  892. if ((ret = ff_alloc_packet(avpkt, buf_size)))
  893. return ret;
  894. /* Encoders using AVCodec.encode() that support
  895. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  896. the smaller size when encoding the last frame.
  897. This code can be removed once all encoders supporting
  898. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  899. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  900. nb_samples < avctx->frame_size) {
  901. fs_tmp = avctx->frame_size;
  902. avctx->frame_size = nb_samples;
  903. }
  904. /* encode the frame */
  905. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  906. frame ? frame->data[0] : NULL);
  907. if (ret >= 0) {
  908. if (!ret) {
  909. /* no output. if the packet data was allocated by libavcodec,
  910. free it */
  911. if (!user_packet)
  912. av_freep(&avpkt->data);
  913. } else {
  914. if (avctx->coded_frame)
  915. avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
  916. /* Set duration for final small packet. This can be removed
  917. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  918. encode2() */
  919. if (fs_tmp) {
  920. avpkt->duration = ff_samples_to_time_base(avctx,
  921. avctx->frame_size);
  922. }
  923. }
  924. avpkt->size = ret;
  925. *got_packet_ptr = (ret > 0);
  926. ret = 0;
  927. }
  928. if (fs_tmp)
  929. avctx->frame_size = fs_tmp;
  930. }
  931. if (!ret)
  932. avctx->frame_number++;
  933. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  934. this needs to be moved to the encoders, but for now we can do it
  935. here to simplify things */
  936. avpkt->flags |= AV_PKT_FLAG_KEY;
  937. return ret;
  938. }
  939. #if FF_API_OLD_DECODE_AUDIO
  940. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  941. uint8_t *buf, int buf_size,
  942. const short *samples)
  943. {
  944. AVPacket pkt;
  945. AVFrame frame0;
  946. AVFrame *frame;
  947. int ret, samples_size, got_packet;
  948. av_init_packet(&pkt);
  949. pkt.data = buf;
  950. pkt.size = buf_size;
  951. if (samples) {
  952. frame = &frame0;
  953. avcodec_get_frame_defaults(frame);
  954. if (avctx->frame_size) {
  955. frame->nb_samples = avctx->frame_size;
  956. } else {
  957. /* if frame_size is not set, the number of samples must be
  958. calculated from the buffer size */
  959. int64_t nb_samples;
  960. if (!av_get_bits_per_sample(avctx->codec_id)) {
  961. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  962. "support this codec\n");
  963. return AVERROR(EINVAL);
  964. }
  965. nb_samples = (int64_t)buf_size * 8 /
  966. (av_get_bits_per_sample(avctx->codec_id) *
  967. avctx->channels);
  968. if (nb_samples >= INT_MAX)
  969. return AVERROR(EINVAL);
  970. frame->nb_samples = nb_samples;
  971. }
  972. /* it is assumed that the samples buffer is large enough based on the
  973. relevant parameters */
  974. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  975. frame->nb_samples,
  976. avctx->sample_fmt, 1);
  977. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  978. avctx->sample_fmt,
  979. samples, samples_size, 1)))
  980. return ret;
  981. /* fabricate frame pts from sample count.
  982. this is needed because the avcodec_encode_audio() API does not have
  983. a way for the user to provide pts */
  984. if(avctx->sample_rate && avctx->time_base.num)
  985. frame->pts = ff_samples_to_time_base(avctx,
  986. avctx->internal->sample_count);
  987. else
  988. frame->pts = AV_NOPTS_VALUE;
  989. avctx->internal->sample_count += frame->nb_samples;
  990. } else {
  991. frame = NULL;
  992. }
  993. got_packet = 0;
  994. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  995. if (!ret && got_packet && avctx->coded_frame) {
  996. avctx->coded_frame->pts = pkt.pts;
  997. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  998. }
  999. /* free any side data since we cannot return it */
  1000. ff_packet_free_side_data(&pkt);
  1001. if (frame && frame->extended_data != frame->data)
  1002. av_freep(&frame->extended_data);
  1003. return ret ? ret : pkt.size;
  1004. }
  1005. #endif
  1006. #if FF_API_OLD_ENCODE_VIDEO
  1007. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1008. const AVFrame *pict)
  1009. {
  1010. AVPacket pkt;
  1011. int ret, got_packet = 0;
  1012. if(buf_size < FF_MIN_BUFFER_SIZE){
  1013. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1014. return -1;
  1015. }
  1016. av_init_packet(&pkt);
  1017. pkt.data = buf;
  1018. pkt.size = buf_size;
  1019. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1020. if (!ret && got_packet && avctx->coded_frame) {
  1021. avctx->coded_frame->pts = pkt.pts;
  1022. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1023. }
  1024. /* free any side data since we cannot return it */
  1025. if (pkt.side_data_elems > 0) {
  1026. int i;
  1027. for (i = 0; i < pkt.side_data_elems; i++)
  1028. av_free(pkt.side_data[i].data);
  1029. av_freep(&pkt.side_data);
  1030. pkt.side_data_elems = 0;
  1031. }
  1032. return ret ? ret : pkt.size;
  1033. }
  1034. #endif
  1035. #define MAX_CODED_FRAME_SIZE(width, height)\
  1036. (9*(width)*(height) + FF_MIN_BUFFER_SIZE)
  1037. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1038. AVPacket *avpkt,
  1039. const AVFrame *frame,
  1040. int *got_packet_ptr)
  1041. {
  1042. int ret;
  1043. int user_packet = !!avpkt->data;
  1044. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1045. av_init_packet(avpkt);
  1046. avpkt->size = 0;
  1047. *got_packet_ptr = 0;
  1048. return 0;
  1049. }
  1050. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1051. return AVERROR(EINVAL);
  1052. if (avctx->codec->encode2) {
  1053. *got_packet_ptr = 0;
  1054. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1055. if (!ret) {
  1056. if (!*got_packet_ptr)
  1057. avpkt->size = 0;
  1058. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1059. avpkt->pts = avpkt->dts = frame->pts;
  1060. }
  1061. } else {
  1062. /* for compatibility with encoders not supporting encode2(), we need to
  1063. allocate a packet buffer if the user has not provided one or check
  1064. the size otherwise */
  1065. int buf_size = avpkt->size;
  1066. if (!user_packet)
  1067. buf_size = MAX_CODED_FRAME_SIZE(avctx->width, avctx->height);
  1068. if ((ret = ff_alloc_packet(avpkt, buf_size)))
  1069. return ret;
  1070. /* encode the frame */
  1071. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size, frame);
  1072. if (ret >= 0) {
  1073. if (!ret) {
  1074. /* no output. if the packet data was allocated by libavcodec,
  1075. free it */
  1076. if (!user_packet)
  1077. av_freep(&avpkt->data);
  1078. } else if (avctx->coded_frame) {
  1079. avpkt->pts = avctx->coded_frame->pts;
  1080. avpkt->flags |= AV_PKT_FLAG_KEY*!!avctx->coded_frame->key_frame;
  1081. }
  1082. avpkt->size = ret;
  1083. *got_packet_ptr = (ret > 0);
  1084. ret = 0;
  1085. }
  1086. }
  1087. if (!ret)
  1088. avctx->frame_number++;
  1089. emms_c();
  1090. return ret;
  1091. }
  1092. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1093. const AVSubtitle *sub)
  1094. {
  1095. int ret;
  1096. if(sub->start_display_time) {
  1097. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1098. return -1;
  1099. }
  1100. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  1101. avctx->frame_number++;
  1102. return ret;
  1103. }
  1104. /**
  1105. * Attempt to guess proper monotonic timestamps for decoded video frames
  1106. * which might have incorrect times. Input timestamps may wrap around, in
  1107. * which case the output will as well.
  1108. *
  1109. * @param pts the pts field of the decoded AVPacket, as passed through
  1110. * AVFrame.pkt_pts
  1111. * @param dts the dts field of the decoded AVPacket
  1112. * @return one of the input values, may be AV_NOPTS_VALUE
  1113. */
  1114. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1115. int64_t reordered_pts, int64_t dts)
  1116. {
  1117. int64_t pts = AV_NOPTS_VALUE;
  1118. if (dts != AV_NOPTS_VALUE) {
  1119. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1120. ctx->pts_correction_last_dts = dts;
  1121. }
  1122. if (reordered_pts != AV_NOPTS_VALUE) {
  1123. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1124. ctx->pts_correction_last_pts = reordered_pts;
  1125. }
  1126. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1127. && reordered_pts != AV_NOPTS_VALUE)
  1128. pts = reordered_pts;
  1129. else
  1130. pts = dts;
  1131. return pts;
  1132. }
  1133. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1134. {
  1135. int size = 0;
  1136. const uint8_t *data;
  1137. uint32_t flags;
  1138. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1139. return;
  1140. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1141. if (!data || size < 4)
  1142. return;
  1143. flags = bytestream_get_le32(&data);
  1144. size -= 4;
  1145. if (size < 4) /* Required for any of the changes */
  1146. return;
  1147. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1148. avctx->channels = bytestream_get_le32(&data);
  1149. size -= 4;
  1150. }
  1151. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1152. if (size < 8)
  1153. return;
  1154. avctx->channel_layout = bytestream_get_le64(&data);
  1155. size -= 8;
  1156. }
  1157. if (size < 4)
  1158. return;
  1159. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1160. avctx->sample_rate = bytestream_get_le32(&data);
  1161. size -= 4;
  1162. }
  1163. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1164. if (size < 8)
  1165. return;
  1166. avctx->width = bytestream_get_le32(&data);
  1167. avctx->height = bytestream_get_le32(&data);
  1168. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1169. size -= 8;
  1170. }
  1171. }
  1172. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1173. int *got_picture_ptr,
  1174. const AVPacket *avpkt)
  1175. {
  1176. int ret;
  1177. // copy to ensure we do not change avpkt
  1178. AVPacket tmp = *avpkt;
  1179. *got_picture_ptr= 0;
  1180. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1181. return -1;
  1182. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  1183. int did_split = av_packet_split_side_data(&tmp);
  1184. apply_param_change(avctx, &tmp);
  1185. avctx->pkt = &tmp;
  1186. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1187. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1188. &tmp);
  1189. else {
  1190. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1191. &tmp);
  1192. picture->pkt_dts= avpkt->dts;
  1193. if(!avctx->has_b_frames){
  1194. picture->pkt_pos= avpkt->pos;
  1195. }
  1196. //FIXME these should be under if(!avctx->has_b_frames)
  1197. if (!picture->sample_aspect_ratio.num)
  1198. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1199. if (!picture->width)
  1200. picture->width = avctx->width;
  1201. if (!picture->height)
  1202. picture->height = avctx->height;
  1203. if (picture->format == PIX_FMT_NONE)
  1204. picture->format = avctx->pix_fmt;
  1205. }
  1206. emms_c(); //needed to avoid an emms_c() call before every return;
  1207. avctx->pkt = NULL;
  1208. if (did_split)
  1209. ff_packet_free_side_data(&tmp);
  1210. if (*got_picture_ptr){
  1211. avctx->frame_number++;
  1212. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1213. picture->pkt_pts,
  1214. picture->pkt_dts);
  1215. }
  1216. }else
  1217. ret= 0;
  1218. return ret;
  1219. }
  1220. #if FF_API_OLD_DECODE_AUDIO
  1221. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1222. int *frame_size_ptr,
  1223. AVPacket *avpkt)
  1224. {
  1225. AVFrame frame;
  1226. int ret, got_frame = 0;
  1227. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1228. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1229. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1230. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1231. "avcodec_decode_audio4()\n");
  1232. avctx->get_buffer = avcodec_default_get_buffer;
  1233. avctx->release_buffer = avcodec_default_release_buffer;
  1234. }
  1235. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1236. if (ret >= 0 && got_frame) {
  1237. int ch, plane_size;
  1238. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1239. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1240. frame.nb_samples,
  1241. avctx->sample_fmt, 1);
  1242. if (*frame_size_ptr < data_size) {
  1243. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1244. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1245. return AVERROR(EINVAL);
  1246. }
  1247. memcpy(samples, frame.extended_data[0], plane_size);
  1248. if (planar && avctx->channels > 1) {
  1249. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1250. for (ch = 1; ch < avctx->channels; ch++) {
  1251. memcpy(out, frame.extended_data[ch], plane_size);
  1252. out += plane_size;
  1253. }
  1254. }
  1255. *frame_size_ptr = data_size;
  1256. } else {
  1257. *frame_size_ptr = 0;
  1258. }
  1259. return ret;
  1260. }
  1261. #endif
  1262. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1263. AVFrame *frame,
  1264. int *got_frame_ptr,
  1265. AVPacket *avpkt)
  1266. {
  1267. int ret = 0;
  1268. *got_frame_ptr = 0;
  1269. if (!avpkt->data && avpkt->size) {
  1270. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1271. return AVERROR(EINVAL);
  1272. }
  1273. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1274. av_packet_split_side_data(avpkt);
  1275. apply_param_change(avctx, avpkt);
  1276. avctx->pkt = avpkt;
  1277. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1278. if (ret >= 0 && *got_frame_ptr) {
  1279. avctx->frame_number++;
  1280. frame->pkt_dts = avpkt->dts;
  1281. if (frame->format == AV_SAMPLE_FMT_NONE)
  1282. frame->format = avctx->sample_fmt;
  1283. }
  1284. }
  1285. return ret;
  1286. }
  1287. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1288. int *got_sub_ptr,
  1289. AVPacket *avpkt)
  1290. {
  1291. int ret;
  1292. avctx->pkt = avpkt;
  1293. *got_sub_ptr = 0;
  1294. avcodec_get_subtitle_defaults(sub);
  1295. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1296. if (*got_sub_ptr)
  1297. avctx->frame_number++;
  1298. return ret;
  1299. }
  1300. void avsubtitle_free(AVSubtitle *sub)
  1301. {
  1302. int i;
  1303. for (i = 0; i < sub->num_rects; i++)
  1304. {
  1305. av_freep(&sub->rects[i]->pict.data[0]);
  1306. av_freep(&sub->rects[i]->pict.data[1]);
  1307. av_freep(&sub->rects[i]->pict.data[2]);
  1308. av_freep(&sub->rects[i]->pict.data[3]);
  1309. av_freep(&sub->rects[i]->text);
  1310. av_freep(&sub->rects[i]->ass);
  1311. av_freep(&sub->rects[i]);
  1312. }
  1313. av_freep(&sub->rects);
  1314. memset(sub, 0, sizeof(AVSubtitle));
  1315. }
  1316. av_cold int avcodec_close(AVCodecContext *avctx)
  1317. {
  1318. /* If there is a user-supplied mutex locking routine, call it. */
  1319. if (ff_lockmgr_cb) {
  1320. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1321. return -1;
  1322. }
  1323. entangled_thread_counter++;
  1324. if(entangled_thread_counter != 1){
  1325. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1326. entangled_thread_counter--;
  1327. return -1;
  1328. }
  1329. if (avcodec_is_open(avctx)) {
  1330. if (HAVE_THREADS && avctx->thread_opaque)
  1331. ff_thread_free(avctx);
  1332. if (avctx->codec && avctx->codec->close)
  1333. avctx->codec->close(avctx);
  1334. avcodec_default_free_buffers(avctx);
  1335. avctx->coded_frame = NULL;
  1336. av_freep(&avctx->internal);
  1337. }
  1338. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1339. av_opt_free(avctx->priv_data);
  1340. av_opt_free(avctx);
  1341. av_freep(&avctx->priv_data);
  1342. if (codec_is_encoder(avctx->codec))
  1343. av_freep(&avctx->extradata);
  1344. avctx->codec = NULL;
  1345. avctx->active_thread_type = 0;
  1346. entangled_thread_counter--;
  1347. /* Release any user-supplied mutex. */
  1348. if (ff_lockmgr_cb) {
  1349. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1350. }
  1351. return 0;
  1352. }
  1353. static enum CodecID remap_deprecated_codec_id(enum CodecID id)
  1354. {
  1355. switch(id){
  1356. //This is for future deprecatec codec ids, its empty since
  1357. //last major bump but will fill up again over time, please dont remove it
  1358. // case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
  1359. default : return id;
  1360. }
  1361. }
  1362. AVCodec *avcodec_find_encoder(enum CodecID id)
  1363. {
  1364. AVCodec *p, *experimental=NULL;
  1365. p = first_avcodec;
  1366. id= remap_deprecated_codec_id(id);
  1367. while (p) {
  1368. if (codec_is_encoder(p) && p->id == id) {
  1369. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1370. experimental = p;
  1371. } else
  1372. return p;
  1373. }
  1374. p = p->next;
  1375. }
  1376. return experimental;
  1377. }
  1378. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1379. {
  1380. AVCodec *p;
  1381. if (!name)
  1382. return NULL;
  1383. p = first_avcodec;
  1384. while (p) {
  1385. if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1386. return p;
  1387. p = p->next;
  1388. }
  1389. return NULL;
  1390. }
  1391. AVCodec *avcodec_find_decoder(enum CodecID id)
  1392. {
  1393. AVCodec *p, *experimental=NULL;
  1394. p = first_avcodec;
  1395. id= remap_deprecated_codec_id(id);
  1396. while (p) {
  1397. if (codec_is_decoder(p) && p->id == id) {
  1398. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1399. experimental = p;
  1400. } else
  1401. return p;
  1402. }
  1403. p = p->next;
  1404. }
  1405. return experimental;
  1406. }
  1407. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1408. {
  1409. AVCodec *p;
  1410. if (!name)
  1411. return NULL;
  1412. p = first_avcodec;
  1413. while (p) {
  1414. if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1415. return p;
  1416. p = p->next;
  1417. }
  1418. return NULL;
  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. }