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.

1186 lines
34KB

  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/avstring.h"
  27. #include "libavutil/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavcore/imgutils.h"
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "opt.h"
  34. #include "imgconvert.h"
  35. #include "audioconvert.h"
  36. #include "internal.h"
  37. #include <stdlib.h>
  38. #include <stdarg.h>
  39. #include <limits.h>
  40. #include <float.h>
  41. static int volatile entangled_thread_counter=0;
  42. int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  43. static void *codec_mutex;
  44. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  45. {
  46. if(min_size < *size)
  47. return ptr;
  48. *size= FFMAX(17*min_size/16 + 32, min_size);
  49. ptr= av_realloc(ptr, *size);
  50. 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
  51. *size= 0;
  52. return ptr;
  53. }
  54. void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size)
  55. {
  56. void **p = ptr;
  57. if (min_size < *size)
  58. return;
  59. *size= FFMAX(17*min_size/16 + 32, min_size);
  60. av_free(*p);
  61. *p = av_malloc(*size);
  62. if (!*p) *size = 0;
  63. }
  64. /* encoder management */
  65. static AVCodec *first_avcodec = NULL;
  66. AVCodec *av_codec_next(AVCodec *c){
  67. if(c) return c->next;
  68. else return first_avcodec;
  69. }
  70. void avcodec_register(AVCodec *codec)
  71. {
  72. AVCodec **p;
  73. avcodec_init();
  74. p = &first_avcodec;
  75. while (*p != NULL) p = &(*p)->next;
  76. *p = codec;
  77. codec->next = NULL;
  78. }
  79. #if LIBAVCODEC_VERSION_MAJOR < 53
  80. void register_avcodec(AVCodec *codec)
  81. {
  82. avcodec_register(codec);
  83. }
  84. #endif
  85. unsigned avcodec_get_edge_width(void)
  86. {
  87. return EDGE_WIDTH;
  88. }
  89. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  90. s->coded_width = width;
  91. s->coded_height= height;
  92. s->width = -((-width )>>s->lowres);
  93. s->height= -((-height)>>s->lowres);
  94. }
  95. typedef struct InternalBuffer{
  96. int last_pic_num;
  97. uint8_t *base[4];
  98. uint8_t *data[4];
  99. int linesize[4];
  100. int width, height;
  101. enum PixelFormat pix_fmt;
  102. }InternalBuffer;
  103. #define INTERNAL_BUFFER_SIZE 32
  104. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  105. int w_align= 1;
  106. int h_align= 1;
  107. switch(s->pix_fmt){
  108. case PIX_FMT_YUV420P:
  109. case PIX_FMT_YUYV422:
  110. case PIX_FMT_UYVY422:
  111. case PIX_FMT_YUV422P:
  112. case PIX_FMT_YUV440P:
  113. case PIX_FMT_YUV444P:
  114. case PIX_FMT_GRAY8:
  115. case PIX_FMT_GRAY16BE:
  116. case PIX_FMT_GRAY16LE:
  117. case PIX_FMT_YUVJ420P:
  118. case PIX_FMT_YUVJ422P:
  119. case PIX_FMT_YUVJ440P:
  120. case PIX_FMT_YUVJ444P:
  121. case PIX_FMT_YUVA420P:
  122. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  123. h_align= 16;
  124. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP)
  125. h_align= 32; // interlaced is rounded up to 2 MBs
  126. break;
  127. case PIX_FMT_YUV411P:
  128. case PIX_FMT_UYYVYY411:
  129. w_align=32;
  130. h_align=8;
  131. break;
  132. case PIX_FMT_YUV410P:
  133. if(s->codec_id == CODEC_ID_SVQ1){
  134. w_align=64;
  135. h_align=64;
  136. }
  137. case PIX_FMT_RGB555:
  138. if(s->codec_id == CODEC_ID_RPZA){
  139. w_align=4;
  140. h_align=4;
  141. }
  142. case PIX_FMT_PAL8:
  143. case PIX_FMT_BGR8:
  144. case PIX_FMT_RGB8:
  145. if(s->codec_id == CODEC_ID_SMC){
  146. w_align=4;
  147. h_align=4;
  148. }
  149. break;
  150. case PIX_FMT_BGR24:
  151. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  152. w_align=4;
  153. h_align=4;
  154. }
  155. break;
  156. default:
  157. w_align= 1;
  158. h_align= 1;
  159. break;
  160. }
  161. *width = FFALIGN(*width , w_align);
  162. *height= FFALIGN(*height, h_align);
  163. if(s->codec_id == CODEC_ID_H264)
  164. *height+=2; // some of the optimized chroma MC reads one line too much
  165. linesize_align[0] =
  166. linesize_align[1] =
  167. linesize_align[2] =
  168. linesize_align[3] = STRIDE_ALIGN;
  169. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  170. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  171. //picture size unneccessarily in some cases. The solution here is not
  172. //pretty and better ideas are welcome!
  173. #if HAVE_MMX
  174. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  175. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  176. s->codec_id == CODEC_ID_VP6A) {
  177. linesize_align[0] =
  178. linesize_align[1] =
  179. linesize_align[2] = 16;
  180. }
  181. #endif
  182. }
  183. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  184. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  185. int linesize_align[4];
  186. int align;
  187. avcodec_align_dimensions2(s, width, height, linesize_align);
  188. align = FFMAX(linesize_align[0], linesize_align[3]);
  189. linesize_align[1] <<= chroma_shift;
  190. linesize_align[2] <<= chroma_shift;
  191. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  192. *width=FFALIGN(*width, align);
  193. }
  194. #if LIBAVCODEC_VERSION_MAJOR < 53
  195. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  196. return av_check_image_size(w, h, 0, av_log_ctx);
  197. }
  198. #endif
  199. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  200. int i;
  201. int w= s->width;
  202. int h= s->height;
  203. InternalBuffer *buf;
  204. int *picture_number;
  205. if(pic->data[0]!=NULL) {
  206. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  207. return -1;
  208. }
  209. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  210. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  211. return -1;
  212. }
  213. if(av_check_image_size(w, h, 0, s))
  214. return -1;
  215. if(s->internal_buffer==NULL){
  216. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  217. }
  218. #if 0
  219. s->internal_buffer= av_fast_realloc(
  220. s->internal_buffer,
  221. &s->internal_buffer_size,
  222. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  223. );
  224. #endif
  225. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  226. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  227. (*picture_number)++;
  228. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  229. for(i=0; i<4; i++){
  230. av_freep(&buf->base[i]);
  231. buf->data[i]= NULL;
  232. }
  233. }
  234. if(buf->base[0]){
  235. pic->age= *picture_number - buf->last_pic_num;
  236. buf->last_pic_num= *picture_number;
  237. }else{
  238. int h_chroma_shift, v_chroma_shift;
  239. int size[4] = {0};
  240. int tmpsize;
  241. int unaligned;
  242. AVPicture picture;
  243. int stride_align[4];
  244. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  245. avcodec_align_dimensions2(s, &w, &h, stride_align);
  246. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  247. w+= EDGE_WIDTH*2;
  248. h+= EDGE_WIDTH*2;
  249. }
  250. do {
  251. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  252. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  253. av_fill_image_linesizes(picture.linesize, s->pix_fmt, w);
  254. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  255. w += w & ~(w-1);
  256. unaligned = 0;
  257. for (i=0; i<4; i++){
  258. unaligned |= picture.linesize[i] % stride_align[i];
  259. }
  260. } while (unaligned);
  261. tmpsize = av_fill_image_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  262. if (tmpsize < 0)
  263. return -1;
  264. for (i=0; i<3 && picture.data[i+1]; i++)
  265. size[i] = picture.data[i+1] - picture.data[i];
  266. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  267. buf->last_pic_num= -256*256*256*64;
  268. memset(buf->base, 0, sizeof(buf->base));
  269. memset(buf->data, 0, sizeof(buf->data));
  270. for(i=0; i<4 && size[i]; i++){
  271. const int h_shift= i==0 ? 0 : h_chroma_shift;
  272. const int v_shift= i==0 ? 0 : v_chroma_shift;
  273. buf->linesize[i]= picture.linesize[i];
  274. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  275. if(buf->base[i]==NULL) return -1;
  276. memset(buf->base[i], 128, size[i]);
  277. // no edge if EDGE EMU or not planar YUV
  278. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  279. buf->data[i] = buf->base[i];
  280. else
  281. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  282. }
  283. if(size[1] && !size[2])
  284. ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
  285. buf->width = s->width;
  286. buf->height = s->height;
  287. buf->pix_fmt= s->pix_fmt;
  288. pic->age= 256*256*256*64;
  289. }
  290. pic->type= FF_BUFFER_TYPE_INTERNAL;
  291. for(i=0; i<4; i++){
  292. pic->base[i]= buf->base[i];
  293. pic->data[i]= buf->data[i];
  294. pic->linesize[i]= buf->linesize[i];
  295. }
  296. s->internal_buffer_count++;
  297. pic->reordered_opaque= s->reordered_opaque;
  298. if(s->debug&FF_DEBUG_BUFFERS)
  299. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  300. return 0;
  301. }
  302. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  303. int i;
  304. InternalBuffer *buf, *last;
  305. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  306. assert(s->internal_buffer_count);
  307. buf = NULL; /* avoids warning */
  308. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  309. buf= &((InternalBuffer*)s->internal_buffer)[i];
  310. if(buf->data[0] == pic->data[0])
  311. break;
  312. }
  313. assert(i < s->internal_buffer_count);
  314. s->internal_buffer_count--;
  315. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  316. FFSWAP(InternalBuffer, *buf, *last);
  317. for(i=0; i<4; i++){
  318. pic->data[i]=NULL;
  319. // pic->base[i]=NULL;
  320. }
  321. //printf("R%X\n", pic->opaque);
  322. if(s->debug&FF_DEBUG_BUFFERS)
  323. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  324. }
  325. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  326. AVFrame temp_pic;
  327. int i;
  328. /* If no picture return a new buffer */
  329. if(pic->data[0] == NULL) {
  330. /* We will copy from buffer, so must be readable */
  331. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  332. return s->get_buffer(s, pic);
  333. }
  334. /* If internal buffer type return the same buffer */
  335. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  336. pic->reordered_opaque= s->reordered_opaque;
  337. return 0;
  338. }
  339. /*
  340. * Not internal type and reget_buffer not overridden, emulate cr buffer
  341. */
  342. temp_pic = *pic;
  343. for(i = 0; i < 4; i++)
  344. pic->data[i] = pic->base[i] = NULL;
  345. pic->opaque = NULL;
  346. /* Allocate new frame */
  347. if (s->get_buffer(s, pic))
  348. return -1;
  349. /* Copy image data from old buffer to new buffer */
  350. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  351. s->height);
  352. s->release_buffer(s, &temp_pic); // Release old frame
  353. return 0;
  354. }
  355. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  356. int i;
  357. for(i=0; i<count; i++){
  358. int r= func(c, (char*)arg + i*size);
  359. if(ret) ret[i]= r;
  360. }
  361. return 0;
  362. }
  363. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  364. int i;
  365. for(i=0; i<count; i++){
  366. int r= func(c, arg, i, 0);
  367. if(ret) ret[i]= r;
  368. }
  369. return 0;
  370. }
  371. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  372. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  373. ++fmt;
  374. return fmt[0];
  375. }
  376. void avcodec_get_frame_defaults(AVFrame *pic){
  377. memset(pic, 0, sizeof(AVFrame));
  378. pic->pts= AV_NOPTS_VALUE;
  379. pic->key_frame= 1;
  380. }
  381. AVFrame *avcodec_alloc_frame(void){
  382. AVFrame *pic= av_malloc(sizeof(AVFrame));
  383. if(pic==NULL) return NULL;
  384. avcodec_get_frame_defaults(pic);
  385. return pic;
  386. }
  387. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  388. {
  389. int ret= -1;
  390. /* If there is a user-supplied mutex locking routine, call it. */
  391. if (ff_lockmgr_cb) {
  392. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  393. return -1;
  394. }
  395. entangled_thread_counter++;
  396. if(entangled_thread_counter != 1){
  397. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  398. goto end;
  399. }
  400. if(avctx->codec || !codec)
  401. goto end;
  402. if (codec->priv_data_size > 0) {
  403. avctx->priv_data = av_mallocz(codec->priv_data_size);
  404. if (!avctx->priv_data) {
  405. ret = AVERROR(ENOMEM);
  406. goto end;
  407. }
  408. } else {
  409. avctx->priv_data = NULL;
  410. }
  411. if(avctx->coded_width && avctx->coded_height)
  412. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  413. else if(avctx->width && avctx->height)
  414. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  415. #define SANE_NB_CHANNELS 128U
  416. if (((avctx->coded_width || avctx->coded_height)
  417. && av_check_image_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  418. || avctx->channels > SANE_NB_CHANNELS) {
  419. ret = AVERROR(EINVAL);
  420. goto free_and_end;
  421. }
  422. avctx->codec = codec;
  423. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  424. avctx->codec_id == CODEC_ID_NONE) {
  425. avctx->codec_type = codec->type;
  426. avctx->codec_id = codec->id;
  427. }
  428. if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
  429. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  430. goto free_and_end;
  431. }
  432. avctx->frame_number = 0;
  433. if(avctx->codec->init){
  434. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
  435. avctx->codec->max_lowres < avctx->lowres){
  436. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  437. avctx->codec->max_lowres);
  438. goto free_and_end;
  439. }
  440. ret = avctx->codec->init(avctx);
  441. if (ret < 0) {
  442. goto free_and_end;
  443. }
  444. }
  445. ret=0;
  446. end:
  447. entangled_thread_counter--;
  448. /* Release any user-supplied mutex. */
  449. if (ff_lockmgr_cb) {
  450. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  451. }
  452. return ret;
  453. free_and_end:
  454. av_freep(&avctx->priv_data);
  455. avctx->codec= NULL;
  456. goto end;
  457. }
  458. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  459. const short *samples)
  460. {
  461. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  462. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  463. return -1;
  464. }
  465. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  466. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  467. avctx->frame_number++;
  468. return ret;
  469. }else
  470. return 0;
  471. }
  472. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  473. const AVFrame *pict)
  474. {
  475. if(buf_size < FF_MIN_BUFFER_SIZE){
  476. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  477. return -1;
  478. }
  479. if(av_check_image_size(avctx->width, avctx->height, 0, avctx))
  480. return -1;
  481. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  482. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  483. avctx->frame_number++;
  484. emms_c(); //needed to avoid an emms_c() call before every return;
  485. return ret;
  486. }else
  487. return 0;
  488. }
  489. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  490. const AVSubtitle *sub)
  491. {
  492. int ret;
  493. if(sub->start_display_time) {
  494. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  495. return -1;
  496. }
  497. if(sub->num_rects == 0 || !sub->rects)
  498. return -1;
  499. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  500. avctx->frame_number++;
  501. return ret;
  502. }
  503. #if LIBAVCODEC_VERSION_MAJOR < 53
  504. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  505. int *got_picture_ptr,
  506. const uint8_t *buf, int buf_size)
  507. {
  508. AVPacket avpkt;
  509. av_init_packet(&avpkt);
  510. avpkt.data = buf;
  511. avpkt.size = buf_size;
  512. // HACK for CorePNG to decode as normal PNG by default
  513. avpkt.flags = AV_PKT_FLAG_KEY;
  514. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  515. }
  516. #endif
  517. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  518. int *got_picture_ptr,
  519. AVPacket *avpkt)
  520. {
  521. int ret;
  522. *got_picture_ptr= 0;
  523. if((avctx->coded_width||avctx->coded_height) && av_check_image_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  524. return -1;
  525. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  526. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  527. avpkt);
  528. emms_c(); //needed to avoid an emms_c() call before every return;
  529. if (*got_picture_ptr)
  530. avctx->frame_number++;
  531. }else
  532. ret= 0;
  533. return ret;
  534. }
  535. #if LIBAVCODEC_VERSION_MAJOR < 53
  536. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  537. int *frame_size_ptr,
  538. const uint8_t *buf, int buf_size)
  539. {
  540. AVPacket avpkt;
  541. av_init_packet(&avpkt);
  542. avpkt.data = buf;
  543. avpkt.size = buf_size;
  544. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  545. }
  546. #endif
  547. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  548. int *frame_size_ptr,
  549. AVPacket *avpkt)
  550. {
  551. int ret;
  552. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  553. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  554. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  555. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  556. return -1;
  557. }
  558. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  559. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  560. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  561. return -1;
  562. }
  563. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  564. avctx->frame_number++;
  565. }else{
  566. ret= 0;
  567. *frame_size_ptr=0;
  568. }
  569. return ret;
  570. }
  571. #if LIBAVCODEC_VERSION_MAJOR < 53
  572. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  573. int *got_sub_ptr,
  574. const uint8_t *buf, int buf_size)
  575. {
  576. AVPacket avpkt;
  577. av_init_packet(&avpkt);
  578. avpkt.data = buf;
  579. avpkt.size = buf_size;
  580. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  581. }
  582. #endif
  583. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  584. int *got_sub_ptr,
  585. AVPacket *avpkt)
  586. {
  587. int ret;
  588. *got_sub_ptr = 0;
  589. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  590. if (*got_sub_ptr)
  591. avctx->frame_number++;
  592. return ret;
  593. }
  594. void avsubtitle_free(AVSubtitle *sub)
  595. {
  596. int i;
  597. for (i = 0; i < sub->num_rects; i++)
  598. {
  599. av_freep(&sub->rects[i]->pict.data[0]);
  600. av_freep(&sub->rects[i]->pict.data[1]);
  601. av_freep(&sub->rects[i]->pict.data[2]);
  602. av_freep(&sub->rects[i]->pict.data[3]);
  603. av_freep(&sub->rects[i]->text);
  604. av_freep(&sub->rects[i]->ass);
  605. av_freep(&sub->rects[i]);
  606. }
  607. av_freep(&sub->rects);
  608. memset(sub, 0, sizeof(AVSubtitle));
  609. }
  610. av_cold int avcodec_close(AVCodecContext *avctx)
  611. {
  612. /* If there is a user-supplied mutex locking routine, call it. */
  613. if (ff_lockmgr_cb) {
  614. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  615. return -1;
  616. }
  617. entangled_thread_counter++;
  618. if(entangled_thread_counter != 1){
  619. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  620. entangled_thread_counter--;
  621. return -1;
  622. }
  623. if (HAVE_THREADS && avctx->thread_opaque)
  624. avcodec_thread_free(avctx);
  625. if (avctx->codec && avctx->codec->close)
  626. avctx->codec->close(avctx);
  627. avcodec_default_free_buffers(avctx);
  628. avctx->coded_frame = NULL;
  629. av_freep(&avctx->priv_data);
  630. if(avctx->codec && avctx->codec->encode)
  631. av_freep(&avctx->extradata);
  632. avctx->codec = NULL;
  633. entangled_thread_counter--;
  634. /* Release any user-supplied mutex. */
  635. if (ff_lockmgr_cb) {
  636. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  637. }
  638. return 0;
  639. }
  640. AVCodec *avcodec_find_encoder(enum CodecID id)
  641. {
  642. AVCodec *p, *experimental=NULL;
  643. p = first_avcodec;
  644. while (p) {
  645. if (p->encode != NULL && p->id == id) {
  646. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  647. experimental = p;
  648. } else
  649. return p;
  650. }
  651. p = p->next;
  652. }
  653. return experimental;
  654. }
  655. AVCodec *avcodec_find_encoder_by_name(const char *name)
  656. {
  657. AVCodec *p;
  658. if (!name)
  659. return NULL;
  660. p = first_avcodec;
  661. while (p) {
  662. if (p->encode != NULL && strcmp(name,p->name) == 0)
  663. return p;
  664. p = p->next;
  665. }
  666. return NULL;
  667. }
  668. AVCodec *avcodec_find_decoder(enum CodecID id)
  669. {
  670. AVCodec *p;
  671. p = first_avcodec;
  672. while (p) {
  673. if (p->decode != NULL && p->id == id)
  674. return p;
  675. p = p->next;
  676. }
  677. return NULL;
  678. }
  679. AVCodec *avcodec_find_decoder_by_name(const char *name)
  680. {
  681. AVCodec *p;
  682. if (!name)
  683. return NULL;
  684. p = first_avcodec;
  685. while (p) {
  686. if (p->decode != NULL && strcmp(name,p->name) == 0)
  687. return p;
  688. p = p->next;
  689. }
  690. return NULL;
  691. }
  692. static int get_bit_rate(AVCodecContext *ctx)
  693. {
  694. int bit_rate;
  695. int bits_per_sample;
  696. switch(ctx->codec_type) {
  697. case AVMEDIA_TYPE_VIDEO:
  698. case AVMEDIA_TYPE_DATA:
  699. case AVMEDIA_TYPE_SUBTITLE:
  700. case AVMEDIA_TYPE_ATTACHMENT:
  701. bit_rate = ctx->bit_rate;
  702. break;
  703. case AVMEDIA_TYPE_AUDIO:
  704. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  705. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  706. break;
  707. default:
  708. bit_rate = 0;
  709. break;
  710. }
  711. return bit_rate;
  712. }
  713. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  714. {
  715. int i, len, ret = 0;
  716. for (i = 0; i < 4; i++) {
  717. len = snprintf(buf, buf_size,
  718. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  719. buf += len;
  720. buf_size = buf_size > len ? buf_size - len : 0;
  721. ret += len;
  722. codec_tag>>=8;
  723. }
  724. return ret;
  725. }
  726. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  727. {
  728. const char *codec_name;
  729. AVCodec *p;
  730. char buf1[32];
  731. int bitrate;
  732. AVRational display_aspect_ratio;
  733. if (encode)
  734. p = avcodec_find_encoder(enc->codec_id);
  735. else
  736. p = avcodec_find_decoder(enc->codec_id);
  737. if (p) {
  738. codec_name = p->name;
  739. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  740. /* fake mpeg2 transport stream codec (currently not
  741. registered) */
  742. codec_name = "mpeg2ts";
  743. } else if (enc->codec_name[0] != '\0') {
  744. codec_name = enc->codec_name;
  745. } else {
  746. /* output avi tags */
  747. char tag_buf[32];
  748. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  749. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  750. codec_name = buf1;
  751. }
  752. switch(enc->codec_type) {
  753. case AVMEDIA_TYPE_VIDEO:
  754. snprintf(buf, buf_size,
  755. "Video: %s%s",
  756. codec_name, enc->mb_decision ? " (hq)" : "");
  757. if (enc->pix_fmt != PIX_FMT_NONE) {
  758. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  759. ", %s",
  760. avcodec_get_pix_fmt_name(enc->pix_fmt));
  761. }
  762. if (enc->width) {
  763. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  764. ", %dx%d",
  765. enc->width, enc->height);
  766. if (enc->sample_aspect_ratio.num) {
  767. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  768. enc->width*enc->sample_aspect_ratio.num,
  769. enc->height*enc->sample_aspect_ratio.den,
  770. 1024*1024);
  771. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  772. " [PAR %d:%d DAR %d:%d]",
  773. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  774. display_aspect_ratio.num, display_aspect_ratio.den);
  775. }
  776. if(av_log_get_level() >= AV_LOG_DEBUG){
  777. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  778. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  779. ", %d/%d",
  780. enc->time_base.num/g, enc->time_base.den/g);
  781. }
  782. }
  783. if (encode) {
  784. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  785. ", q=%d-%d", enc->qmin, enc->qmax);
  786. }
  787. break;
  788. case AVMEDIA_TYPE_AUDIO:
  789. snprintf(buf, buf_size,
  790. "Audio: %s",
  791. codec_name);
  792. if (enc->sample_rate) {
  793. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  794. ", %d Hz", enc->sample_rate);
  795. }
  796. av_strlcat(buf, ", ", buf_size);
  797. avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  798. if (enc->sample_fmt != SAMPLE_FMT_NONE) {
  799. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  800. ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
  801. }
  802. break;
  803. case AVMEDIA_TYPE_DATA:
  804. snprintf(buf, buf_size, "Data: %s", codec_name);
  805. break;
  806. case AVMEDIA_TYPE_SUBTITLE:
  807. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  808. break;
  809. case AVMEDIA_TYPE_ATTACHMENT:
  810. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  811. break;
  812. default:
  813. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  814. return;
  815. }
  816. if (encode) {
  817. if (enc->flags & CODEC_FLAG_PASS1)
  818. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  819. ", pass 1");
  820. if (enc->flags & CODEC_FLAG_PASS2)
  821. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  822. ", pass 2");
  823. }
  824. bitrate = get_bit_rate(enc);
  825. if (bitrate != 0) {
  826. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  827. ", %d kb/s", bitrate / 1000);
  828. }
  829. }
  830. unsigned avcodec_version( void )
  831. {
  832. return LIBAVCODEC_VERSION_INT;
  833. }
  834. const char *avcodec_configuration(void)
  835. {
  836. return FFMPEG_CONFIGURATION;
  837. }
  838. const char *avcodec_license(void)
  839. {
  840. #define LICENSE_PREFIX "libavcodec license: "
  841. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  842. }
  843. void avcodec_init(void)
  844. {
  845. static int initialized = 0;
  846. if (initialized != 0)
  847. return;
  848. initialized = 1;
  849. dsputil_static_init();
  850. }
  851. void avcodec_flush_buffers(AVCodecContext *avctx)
  852. {
  853. if(avctx->codec->flush)
  854. avctx->codec->flush(avctx);
  855. }
  856. void avcodec_default_free_buffers(AVCodecContext *s){
  857. int i, j;
  858. if(s->internal_buffer==NULL) return;
  859. if (s->internal_buffer_count)
  860. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  861. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  862. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  863. for(j=0; j<4; j++){
  864. av_freep(&buf->base[j]);
  865. buf->data[j]= NULL;
  866. }
  867. }
  868. av_freep(&s->internal_buffer);
  869. s->internal_buffer_count=0;
  870. }
  871. char av_get_pict_type_char(int pict_type){
  872. switch(pict_type){
  873. case FF_I_TYPE: return 'I';
  874. case FF_P_TYPE: return 'P';
  875. case FF_B_TYPE: return 'B';
  876. case FF_S_TYPE: return 'S';
  877. case FF_SI_TYPE:return 'i';
  878. case FF_SP_TYPE:return 'p';
  879. case FF_BI_TYPE:return 'b';
  880. default: return '?';
  881. }
  882. }
  883. int av_get_bits_per_sample(enum CodecID codec_id){
  884. switch(codec_id){
  885. case CODEC_ID_ADPCM_SBPRO_2:
  886. return 2;
  887. case CODEC_ID_ADPCM_SBPRO_3:
  888. return 3;
  889. case CODEC_ID_ADPCM_SBPRO_4:
  890. case CODEC_ID_ADPCM_CT:
  891. case CODEC_ID_ADPCM_IMA_WAV:
  892. case CODEC_ID_ADPCM_MS:
  893. case CODEC_ID_ADPCM_YAMAHA:
  894. return 4;
  895. case CODEC_ID_PCM_ALAW:
  896. case CODEC_ID_PCM_MULAW:
  897. case CODEC_ID_PCM_S8:
  898. case CODEC_ID_PCM_U8:
  899. case CODEC_ID_PCM_ZORK:
  900. return 8;
  901. case CODEC_ID_PCM_S16BE:
  902. case CODEC_ID_PCM_S16LE:
  903. case CODEC_ID_PCM_S16LE_PLANAR:
  904. case CODEC_ID_PCM_U16BE:
  905. case CODEC_ID_PCM_U16LE:
  906. return 16;
  907. case CODEC_ID_PCM_S24DAUD:
  908. case CODEC_ID_PCM_S24BE:
  909. case CODEC_ID_PCM_S24LE:
  910. case CODEC_ID_PCM_U24BE:
  911. case CODEC_ID_PCM_U24LE:
  912. return 24;
  913. case CODEC_ID_PCM_S32BE:
  914. case CODEC_ID_PCM_S32LE:
  915. case CODEC_ID_PCM_U32BE:
  916. case CODEC_ID_PCM_U32LE:
  917. case CODEC_ID_PCM_F32BE:
  918. case CODEC_ID_PCM_F32LE:
  919. return 32;
  920. case CODEC_ID_PCM_F64BE:
  921. case CODEC_ID_PCM_F64LE:
  922. return 64;
  923. default:
  924. return 0;
  925. }
  926. }
  927. int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
  928. switch (sample_fmt) {
  929. case SAMPLE_FMT_U8:
  930. return 8;
  931. case SAMPLE_FMT_S16:
  932. return 16;
  933. case SAMPLE_FMT_S32:
  934. case SAMPLE_FMT_FLT:
  935. return 32;
  936. case SAMPLE_FMT_DBL:
  937. return 64;
  938. default:
  939. return 0;
  940. }
  941. }
  942. #if !HAVE_THREADS
  943. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  944. s->thread_count = thread_count;
  945. return -1;
  946. }
  947. #endif
  948. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  949. {
  950. unsigned int n = 0;
  951. while(v >= 0xff) {
  952. *s++ = 0xff;
  953. v -= 0xff;
  954. n++;
  955. }
  956. *s = v;
  957. n++;
  958. return n;
  959. }
  960. #if LIBAVCODEC_VERSION_MAJOR < 53
  961. #include "libavcore/parseutils.h"
  962. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  963. {
  964. return av_parse_video_size(width_ptr, height_ptr, str);
  965. }
  966. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  967. {
  968. return av_parse_video_rate(frame_rate, arg);
  969. }
  970. #endif
  971. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  972. int i;
  973. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  974. return i;
  975. }
  976. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  977. {
  978. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  979. "version to the newest one from SVN. If the problem still "
  980. "occurs, it means that your file has a feature which has not "
  981. "been implemented.", feature);
  982. if(want_sample)
  983. av_log_ask_for_sample(avc, NULL);
  984. else
  985. av_log(avc, AV_LOG_WARNING, "\n");
  986. }
  987. void av_log_ask_for_sample(void *avc, const char *msg)
  988. {
  989. if (msg)
  990. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  991. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  992. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  993. "and contact the ffmpeg-devel mailing list.\n");
  994. }
  995. static AVHWAccel *first_hwaccel = NULL;
  996. void av_register_hwaccel(AVHWAccel *hwaccel)
  997. {
  998. AVHWAccel **p = &first_hwaccel;
  999. while (*p)
  1000. p = &(*p)->next;
  1001. *p = hwaccel;
  1002. hwaccel->next = NULL;
  1003. }
  1004. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1005. {
  1006. return hwaccel ? hwaccel->next : first_hwaccel;
  1007. }
  1008. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1009. {
  1010. AVHWAccel *hwaccel=NULL;
  1011. while((hwaccel= av_hwaccel_next(hwaccel))){
  1012. if ( hwaccel->id == codec_id
  1013. && hwaccel->pix_fmt == pix_fmt)
  1014. return hwaccel;
  1015. }
  1016. return NULL;
  1017. }
  1018. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1019. {
  1020. if (ff_lockmgr_cb) {
  1021. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1022. return -1;
  1023. }
  1024. ff_lockmgr_cb = cb;
  1025. if (ff_lockmgr_cb) {
  1026. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1027. return -1;
  1028. }
  1029. return 0;
  1030. }
  1031. unsigned int ff_toupper4(unsigned int x)
  1032. {
  1033. return toupper( x &0xFF)
  1034. + (toupper((x>>8 )&0xFF)<<8 )
  1035. + (toupper((x>>16)&0xFF)<<16)
  1036. + (toupper((x>>24)&0xFF)<<24);
  1037. }