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.

1835 lines
56KB

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