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.

1273 lines
36KB

  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 "avcodec.h"
  31. #include "dsputil.h"
  32. #include "opt.h"
  33. #include "imgconvert.h"
  34. #include "audioconvert.h"
  35. #include "internal.h"
  36. #include <stdlib.h>
  37. #include <stdarg.h>
  38. #include <limits.h>
  39. #include <float.h>
  40. static int volatile entangled_thread_counter=0;
  41. int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  42. static void *codec_mutex;
  43. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  44. {
  45. if(min_size < *size)
  46. return ptr;
  47. *size= FFMAX(17*min_size/16 + 32, min_size);
  48. ptr= av_realloc(ptr, *size);
  49. 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
  50. *size= 0;
  51. return ptr;
  52. }
  53. void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size)
  54. {
  55. void **p = ptr;
  56. if (min_size < *size)
  57. return;
  58. *size= FFMAX(17*min_size/16 + 32, min_size);
  59. av_free(*p);
  60. *p = av_malloc(*size);
  61. if (!*p) *size = 0;
  62. }
  63. /* encoder management */
  64. static AVCodec *first_avcodec = NULL;
  65. AVCodec *av_codec_next(AVCodec *c){
  66. if(c) return c->next;
  67. else return first_avcodec;
  68. }
  69. void avcodec_register(AVCodec *codec)
  70. {
  71. AVCodec **p;
  72. avcodec_init();
  73. p = &first_avcodec;
  74. while (*p != NULL) p = &(*p)->next;
  75. *p = codec;
  76. codec->next = NULL;
  77. }
  78. #if LIBAVCODEC_VERSION_MAJOR < 53
  79. void register_avcodec(AVCodec *codec)
  80. {
  81. avcodec_register(codec);
  82. }
  83. #endif
  84. unsigned avcodec_get_edge_width(void)
  85. {
  86. return EDGE_WIDTH;
  87. }
  88. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  89. s->coded_width = width;
  90. s->coded_height= height;
  91. s->width = -((-width )>>s->lowres);
  92. s->height= -((-height)>>s->lowres);
  93. }
  94. typedef struct InternalBuffer{
  95. int last_pic_num;
  96. uint8_t *base[4];
  97. uint8_t *data[4];
  98. int linesize[4];
  99. int width, height;
  100. enum PixelFormat pix_fmt;
  101. }InternalBuffer;
  102. #define INTERNAL_BUFFER_SIZE 32
  103. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  104. int w_align= 1;
  105. int h_align= 1;
  106. switch(s->pix_fmt){
  107. case PIX_FMT_YUV420P:
  108. case PIX_FMT_YUYV422:
  109. case PIX_FMT_UYVY422:
  110. case PIX_FMT_YUV422P:
  111. case PIX_FMT_YUV440P:
  112. case PIX_FMT_YUV444P:
  113. case PIX_FMT_GRAY8:
  114. case PIX_FMT_GRAY16BE:
  115. case PIX_FMT_GRAY16LE:
  116. case PIX_FMT_YUVJ420P:
  117. case PIX_FMT_YUVJ422P:
  118. case PIX_FMT_YUVJ440P:
  119. case PIX_FMT_YUVJ444P:
  120. case PIX_FMT_YUVA420P:
  121. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  122. h_align= 16;
  123. 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)
  124. h_align= 32; // interlaced is rounded up to 2 MBs
  125. break;
  126. case PIX_FMT_YUV411P:
  127. case PIX_FMT_UYYVYY411:
  128. w_align=32;
  129. h_align=8;
  130. break;
  131. case PIX_FMT_YUV410P:
  132. if(s->codec_id == CODEC_ID_SVQ1){
  133. w_align=64;
  134. h_align=64;
  135. }
  136. case PIX_FMT_RGB555:
  137. if(s->codec_id == CODEC_ID_RPZA){
  138. w_align=4;
  139. h_align=4;
  140. }
  141. case PIX_FMT_PAL8:
  142. case PIX_FMT_BGR8:
  143. case PIX_FMT_RGB8:
  144. if(s->codec_id == CODEC_ID_SMC){
  145. w_align=4;
  146. h_align=4;
  147. }
  148. break;
  149. case PIX_FMT_BGR24:
  150. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  151. w_align=4;
  152. h_align=4;
  153. }
  154. break;
  155. default:
  156. w_align= 1;
  157. h_align= 1;
  158. break;
  159. }
  160. *width = FFALIGN(*width , w_align);
  161. *height= FFALIGN(*height, h_align);
  162. if(s->codec_id == CODEC_ID_H264)
  163. *height+=2; // some of the optimized chroma MC reads one line too much
  164. linesize_align[0] =
  165. linesize_align[1] =
  166. linesize_align[2] =
  167. linesize_align[3] = STRIDE_ALIGN;
  168. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  169. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  170. //picture size unneccessarily in some cases. The solution here is not
  171. //pretty and better ideas are welcome!
  172. #if HAVE_MMX
  173. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  174. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  175. s->codec_id == CODEC_ID_VP6A) {
  176. linesize_align[0] =
  177. linesize_align[1] =
  178. linesize_align[2] = 16;
  179. }
  180. #endif
  181. }
  182. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  183. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  184. int linesize_align[4];
  185. int align;
  186. avcodec_align_dimensions2(s, width, height, linesize_align);
  187. align = FFMAX(linesize_align[0], linesize_align[3]);
  188. linesize_align[1] <<= chroma_shift;
  189. linesize_align[2] <<= chroma_shift;
  190. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  191. *width=FFALIGN(*width, align);
  192. }
  193. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  194. if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  195. return 0;
  196. av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
  197. return AVERROR(EINVAL);
  198. }
  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(avcodec_check_dimensions(s,w,h))
  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. ff_fill_linesize(&picture, 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 = ff_fill_pointer(&picture, NULL, s->pix_fmt, h);
  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. && avcodec_check_dimensions(avctx, avctx->coded_width, avctx->coded_height))
  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. ret = avctx->codec->init(avctx);
  435. if (ret < 0) {
  436. goto free_and_end;
  437. }
  438. }
  439. ret=0;
  440. end:
  441. entangled_thread_counter--;
  442. /* Release any user-supplied mutex. */
  443. if (ff_lockmgr_cb) {
  444. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  445. }
  446. return ret;
  447. free_and_end:
  448. av_freep(&avctx->priv_data);
  449. avctx->codec= NULL;
  450. goto end;
  451. }
  452. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  453. const short *samples)
  454. {
  455. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  456. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  457. return -1;
  458. }
  459. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  460. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  461. avctx->frame_number++;
  462. return ret;
  463. }else
  464. return 0;
  465. }
  466. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  467. const AVFrame *pict)
  468. {
  469. if(buf_size < FF_MIN_BUFFER_SIZE){
  470. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  471. return -1;
  472. }
  473. if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
  474. return -1;
  475. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  476. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  477. avctx->frame_number++;
  478. emms_c(); //needed to avoid an emms_c() call before every return;
  479. return ret;
  480. }else
  481. return 0;
  482. }
  483. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  484. const AVSubtitle *sub)
  485. {
  486. int ret;
  487. if(sub->start_display_time) {
  488. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  489. return -1;
  490. }
  491. if(sub->num_rects == 0 || !sub->rects)
  492. return -1;
  493. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  494. avctx->frame_number++;
  495. return ret;
  496. }
  497. #if LIBAVCODEC_VERSION_MAJOR < 53
  498. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  499. int *got_picture_ptr,
  500. const uint8_t *buf, int buf_size)
  501. {
  502. AVPacket avpkt;
  503. av_init_packet(&avpkt);
  504. avpkt.data = buf;
  505. avpkt.size = buf_size;
  506. // HACK for CorePNG to decode as normal PNG by default
  507. avpkt.flags = AV_PKT_FLAG_KEY;
  508. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  509. }
  510. #endif
  511. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  512. int *got_picture_ptr,
  513. AVPacket *avpkt)
  514. {
  515. int ret;
  516. *got_picture_ptr= 0;
  517. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
  518. return -1;
  519. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  520. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  521. avpkt);
  522. emms_c(); //needed to avoid an emms_c() call before every return;
  523. if (*got_picture_ptr)
  524. avctx->frame_number++;
  525. }else
  526. ret= 0;
  527. return ret;
  528. }
  529. #if LIBAVCODEC_VERSION_MAJOR < 53
  530. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  531. int *frame_size_ptr,
  532. const uint8_t *buf, int buf_size)
  533. {
  534. AVPacket avpkt;
  535. av_init_packet(&avpkt);
  536. avpkt.data = buf;
  537. avpkt.size = buf_size;
  538. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  539. }
  540. #endif
  541. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  542. int *frame_size_ptr,
  543. AVPacket *avpkt)
  544. {
  545. int ret;
  546. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  547. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  548. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  549. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  550. return -1;
  551. }
  552. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  553. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  554. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  555. return -1;
  556. }
  557. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  558. avctx->frame_number++;
  559. }else{
  560. ret= 0;
  561. *frame_size_ptr=0;
  562. }
  563. return ret;
  564. }
  565. #if LIBAVCODEC_VERSION_MAJOR < 53
  566. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  567. int *got_sub_ptr,
  568. const uint8_t *buf, int buf_size)
  569. {
  570. AVPacket avpkt;
  571. av_init_packet(&avpkt);
  572. avpkt.data = buf;
  573. avpkt.size = buf_size;
  574. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  575. }
  576. #endif
  577. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  578. int *got_sub_ptr,
  579. AVPacket *avpkt)
  580. {
  581. int ret;
  582. *got_sub_ptr = 0;
  583. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  584. if (*got_sub_ptr)
  585. avctx->frame_number++;
  586. return ret;
  587. }
  588. av_cold int avcodec_close(AVCodecContext *avctx)
  589. {
  590. /* If there is a user-supplied mutex locking routine, call it. */
  591. if (ff_lockmgr_cb) {
  592. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  593. return -1;
  594. }
  595. entangled_thread_counter++;
  596. if(entangled_thread_counter != 1){
  597. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  598. entangled_thread_counter--;
  599. return -1;
  600. }
  601. if (HAVE_THREADS && avctx->thread_opaque)
  602. avcodec_thread_free(avctx);
  603. if (avctx->codec && avctx->codec->close)
  604. avctx->codec->close(avctx);
  605. avcodec_default_free_buffers(avctx);
  606. avctx->coded_frame = NULL;
  607. av_freep(&avctx->priv_data);
  608. if(avctx->codec && avctx->codec->encode)
  609. av_freep(&avctx->extradata);
  610. avctx->codec = NULL;
  611. entangled_thread_counter--;
  612. /* Release any user-supplied mutex. */
  613. if (ff_lockmgr_cb) {
  614. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  615. }
  616. return 0;
  617. }
  618. AVCodec *avcodec_find_encoder(enum CodecID id)
  619. {
  620. AVCodec *p, *experimental=NULL;
  621. p = first_avcodec;
  622. while (p) {
  623. if (p->encode != NULL && p->id == id) {
  624. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  625. experimental = p;
  626. } else
  627. return p;
  628. }
  629. p = p->next;
  630. }
  631. return experimental;
  632. }
  633. AVCodec *avcodec_find_encoder_by_name(const char *name)
  634. {
  635. AVCodec *p;
  636. if (!name)
  637. return NULL;
  638. p = first_avcodec;
  639. while (p) {
  640. if (p->encode != NULL && strcmp(name,p->name) == 0)
  641. return p;
  642. p = p->next;
  643. }
  644. return NULL;
  645. }
  646. AVCodec *avcodec_find_decoder(enum CodecID id)
  647. {
  648. AVCodec *p;
  649. p = first_avcodec;
  650. while (p) {
  651. if (p->decode != NULL && p->id == id)
  652. return p;
  653. p = p->next;
  654. }
  655. return NULL;
  656. }
  657. AVCodec *avcodec_find_decoder_by_name(const char *name)
  658. {
  659. AVCodec *p;
  660. if (!name)
  661. return NULL;
  662. p = first_avcodec;
  663. while (p) {
  664. if (p->decode != NULL && strcmp(name,p->name) == 0)
  665. return p;
  666. p = p->next;
  667. }
  668. return NULL;
  669. }
  670. static int get_bit_rate(AVCodecContext *ctx)
  671. {
  672. int bit_rate;
  673. int bits_per_sample;
  674. switch(ctx->codec_type) {
  675. case AVMEDIA_TYPE_VIDEO:
  676. case AVMEDIA_TYPE_DATA:
  677. case AVMEDIA_TYPE_SUBTITLE:
  678. case AVMEDIA_TYPE_ATTACHMENT:
  679. bit_rate = ctx->bit_rate;
  680. break;
  681. case AVMEDIA_TYPE_AUDIO:
  682. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  683. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  684. break;
  685. default:
  686. bit_rate = 0;
  687. break;
  688. }
  689. return bit_rate;
  690. }
  691. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  692. {
  693. int i, len, ret = 0;
  694. for (i = 0; i < 4; i++) {
  695. len = snprintf(buf, buf_size,
  696. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  697. buf += len;
  698. buf_size = buf_size > len ? buf_size - len : 0;
  699. ret += len;
  700. codec_tag>>=8;
  701. }
  702. return ret;
  703. }
  704. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  705. {
  706. const char *codec_name;
  707. AVCodec *p;
  708. char buf1[32];
  709. int bitrate;
  710. AVRational display_aspect_ratio;
  711. if (encode)
  712. p = avcodec_find_encoder(enc->codec_id);
  713. else
  714. p = avcodec_find_decoder(enc->codec_id);
  715. if (p) {
  716. codec_name = p->name;
  717. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  718. /* fake mpeg2 transport stream codec (currently not
  719. registered) */
  720. codec_name = "mpeg2ts";
  721. } else if (enc->codec_name[0] != '\0') {
  722. codec_name = enc->codec_name;
  723. } else {
  724. /* output avi tags */
  725. char tag_buf[32];
  726. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  727. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  728. codec_name = buf1;
  729. }
  730. switch(enc->codec_type) {
  731. case AVMEDIA_TYPE_VIDEO:
  732. snprintf(buf, buf_size,
  733. "Video: %s%s",
  734. codec_name, enc->mb_decision ? " (hq)" : "");
  735. if (enc->pix_fmt != PIX_FMT_NONE) {
  736. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  737. ", %s",
  738. avcodec_get_pix_fmt_name(enc->pix_fmt));
  739. }
  740. if (enc->width) {
  741. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  742. ", %dx%d",
  743. enc->width, enc->height);
  744. if (enc->sample_aspect_ratio.num) {
  745. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  746. enc->width*enc->sample_aspect_ratio.num,
  747. enc->height*enc->sample_aspect_ratio.den,
  748. 1024*1024);
  749. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  750. " [PAR %d:%d DAR %d:%d]",
  751. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  752. display_aspect_ratio.num, display_aspect_ratio.den);
  753. }
  754. if(av_log_get_level() >= AV_LOG_DEBUG){
  755. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  756. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  757. ", %d/%d",
  758. enc->time_base.num/g, enc->time_base.den/g);
  759. }
  760. }
  761. if (encode) {
  762. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  763. ", q=%d-%d", enc->qmin, enc->qmax);
  764. }
  765. break;
  766. case AVMEDIA_TYPE_AUDIO:
  767. snprintf(buf, buf_size,
  768. "Audio: %s",
  769. codec_name);
  770. if (enc->sample_rate) {
  771. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  772. ", %d Hz", enc->sample_rate);
  773. }
  774. av_strlcat(buf, ", ", buf_size);
  775. avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  776. if (enc->sample_fmt != SAMPLE_FMT_NONE) {
  777. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  778. ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
  779. }
  780. break;
  781. case AVMEDIA_TYPE_DATA:
  782. snprintf(buf, buf_size, "Data: %s", codec_name);
  783. break;
  784. case AVMEDIA_TYPE_SUBTITLE:
  785. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  786. break;
  787. case AVMEDIA_TYPE_ATTACHMENT:
  788. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  789. break;
  790. default:
  791. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  792. return;
  793. }
  794. if (encode) {
  795. if (enc->flags & CODEC_FLAG_PASS1)
  796. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  797. ", pass 1");
  798. if (enc->flags & CODEC_FLAG_PASS2)
  799. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  800. ", pass 2");
  801. }
  802. bitrate = get_bit_rate(enc);
  803. if (bitrate != 0) {
  804. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  805. ", %d kb/s", bitrate / 1000);
  806. }
  807. }
  808. unsigned avcodec_version( void )
  809. {
  810. return LIBAVCODEC_VERSION_INT;
  811. }
  812. const char *avcodec_configuration(void)
  813. {
  814. return FFMPEG_CONFIGURATION;
  815. }
  816. const char *avcodec_license(void)
  817. {
  818. #define LICENSE_PREFIX "libavcodec license: "
  819. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  820. }
  821. void avcodec_init(void)
  822. {
  823. static int initialized = 0;
  824. if (initialized != 0)
  825. return;
  826. initialized = 1;
  827. dsputil_static_init();
  828. }
  829. void avcodec_flush_buffers(AVCodecContext *avctx)
  830. {
  831. if(avctx->codec->flush)
  832. avctx->codec->flush(avctx);
  833. }
  834. void avcodec_default_free_buffers(AVCodecContext *s){
  835. int i, j;
  836. if(s->internal_buffer==NULL) return;
  837. if (s->internal_buffer_count)
  838. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  839. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  840. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  841. for(j=0; j<4; j++){
  842. av_freep(&buf->base[j]);
  843. buf->data[j]= NULL;
  844. }
  845. }
  846. av_freep(&s->internal_buffer);
  847. s->internal_buffer_count=0;
  848. }
  849. char av_get_pict_type_char(int pict_type){
  850. switch(pict_type){
  851. case FF_I_TYPE: return 'I';
  852. case FF_P_TYPE: return 'P';
  853. case FF_B_TYPE: return 'B';
  854. case FF_S_TYPE: return 'S';
  855. case FF_SI_TYPE:return 'i';
  856. case FF_SP_TYPE:return 'p';
  857. case FF_BI_TYPE:return 'b';
  858. default: return '?';
  859. }
  860. }
  861. int av_get_bits_per_sample(enum CodecID codec_id){
  862. switch(codec_id){
  863. case CODEC_ID_ADPCM_SBPRO_2:
  864. return 2;
  865. case CODEC_ID_ADPCM_SBPRO_3:
  866. return 3;
  867. case CODEC_ID_ADPCM_SBPRO_4:
  868. case CODEC_ID_ADPCM_CT:
  869. case CODEC_ID_ADPCM_IMA_WAV:
  870. case CODEC_ID_ADPCM_MS:
  871. case CODEC_ID_ADPCM_YAMAHA:
  872. return 4;
  873. case CODEC_ID_PCM_ALAW:
  874. case CODEC_ID_PCM_MULAW:
  875. case CODEC_ID_PCM_S8:
  876. case CODEC_ID_PCM_U8:
  877. case CODEC_ID_PCM_ZORK:
  878. return 8;
  879. case CODEC_ID_PCM_S16BE:
  880. case CODEC_ID_PCM_S16LE:
  881. case CODEC_ID_PCM_S16LE_PLANAR:
  882. case CODEC_ID_PCM_U16BE:
  883. case CODEC_ID_PCM_U16LE:
  884. return 16;
  885. case CODEC_ID_PCM_S24DAUD:
  886. case CODEC_ID_PCM_S24BE:
  887. case CODEC_ID_PCM_S24LE:
  888. case CODEC_ID_PCM_U24BE:
  889. case CODEC_ID_PCM_U24LE:
  890. return 24;
  891. case CODEC_ID_PCM_S32BE:
  892. case CODEC_ID_PCM_S32LE:
  893. case CODEC_ID_PCM_U32BE:
  894. case CODEC_ID_PCM_U32LE:
  895. case CODEC_ID_PCM_F32BE:
  896. case CODEC_ID_PCM_F32LE:
  897. return 32;
  898. case CODEC_ID_PCM_F64BE:
  899. case CODEC_ID_PCM_F64LE:
  900. return 64;
  901. default:
  902. return 0;
  903. }
  904. }
  905. int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
  906. switch (sample_fmt) {
  907. case SAMPLE_FMT_U8:
  908. return 8;
  909. case SAMPLE_FMT_S16:
  910. return 16;
  911. case SAMPLE_FMT_S32:
  912. case SAMPLE_FMT_FLT:
  913. return 32;
  914. case SAMPLE_FMT_DBL:
  915. return 64;
  916. default:
  917. return 0;
  918. }
  919. }
  920. #if !HAVE_THREADS
  921. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  922. s->thread_count = thread_count;
  923. return -1;
  924. }
  925. #endif
  926. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  927. {
  928. unsigned int n = 0;
  929. while(v >= 0xff) {
  930. *s++ = 0xff;
  931. v -= 0xff;
  932. n++;
  933. }
  934. *s = v;
  935. n++;
  936. return n;
  937. }
  938. typedef struct {
  939. const char *abbr;
  940. int width, height;
  941. } VideoFrameSizeAbbr;
  942. typedef struct {
  943. const char *abbr;
  944. int rate_num, rate_den;
  945. } VideoFrameRateAbbr;
  946. static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
  947. { "ntsc", 720, 480 },
  948. { "pal", 720, 576 },
  949. { "qntsc", 352, 240 }, /* VCD compliant NTSC */
  950. { "qpal", 352, 288 }, /* VCD compliant PAL */
  951. { "sntsc", 640, 480 }, /* square pixel NTSC */
  952. { "spal", 768, 576 }, /* square pixel PAL */
  953. { "film", 352, 240 },
  954. { "ntsc-film", 352, 240 },
  955. { "sqcif", 128, 96 },
  956. { "qcif", 176, 144 },
  957. { "cif", 352, 288 },
  958. { "4cif", 704, 576 },
  959. { "16cif", 1408,1152 },
  960. { "qqvga", 160, 120 },
  961. { "qvga", 320, 240 },
  962. { "vga", 640, 480 },
  963. { "svga", 800, 600 },
  964. { "xga", 1024, 768 },
  965. { "uxga", 1600,1200 },
  966. { "qxga", 2048,1536 },
  967. { "sxga", 1280,1024 },
  968. { "qsxga", 2560,2048 },
  969. { "hsxga", 5120,4096 },
  970. { "wvga", 852, 480 },
  971. { "wxga", 1366, 768 },
  972. { "wsxga", 1600,1024 },
  973. { "wuxga", 1920,1200 },
  974. { "woxga", 2560,1600 },
  975. { "wqsxga", 3200,2048 },
  976. { "wquxga", 3840,2400 },
  977. { "whsxga", 6400,4096 },
  978. { "whuxga", 7680,4800 },
  979. { "cga", 320, 200 },
  980. { "ega", 640, 350 },
  981. { "hd480", 852, 480 },
  982. { "hd720", 1280, 720 },
  983. { "hd1080", 1920,1080 },
  984. };
  985. static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
  986. { "ntsc", 30000, 1001 },
  987. { "pal", 25, 1 },
  988. { "qntsc", 30000, 1001 }, /* VCD compliant NTSC */
  989. { "qpal", 25, 1 }, /* VCD compliant PAL */
  990. { "sntsc", 30000, 1001 }, /* square pixel NTSC */
  991. { "spal", 25, 1 }, /* square pixel PAL */
  992. { "film", 24, 1 },
  993. { "ntsc-film", 24000, 1001 },
  994. };
  995. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  996. {
  997. int i;
  998. int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
  999. char *p;
  1000. int frame_width = 0, frame_height = 0;
  1001. for(i=0;i<n;i++) {
  1002. if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
  1003. frame_width = video_frame_size_abbrs[i].width;
  1004. frame_height = video_frame_size_abbrs[i].height;
  1005. break;
  1006. }
  1007. }
  1008. if (i == n) {
  1009. p = str;
  1010. frame_width = strtol(p, &p, 10);
  1011. if (*p)
  1012. p++;
  1013. frame_height = strtol(p, &p, 10);
  1014. }
  1015. if (frame_width <= 0 || frame_height <= 0)
  1016. return -1;
  1017. *width_ptr = frame_width;
  1018. *height_ptr = frame_height;
  1019. return 0;
  1020. }
  1021. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  1022. {
  1023. int i;
  1024. int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
  1025. char* cp;
  1026. /* First, we check our abbreviation table */
  1027. for (i = 0; i < n; ++i)
  1028. if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
  1029. frame_rate->num = video_frame_rate_abbrs[i].rate_num;
  1030. frame_rate->den = video_frame_rate_abbrs[i].rate_den;
  1031. return 0;
  1032. }
  1033. /* Then, we try to parse it as fraction */
  1034. cp = strchr(arg, '/');
  1035. if (!cp)
  1036. cp = strchr(arg, ':');
  1037. if (cp) {
  1038. char* cpp;
  1039. frame_rate->num = strtol(arg, &cpp, 10);
  1040. if (cpp != arg || cpp == cp)
  1041. frame_rate->den = strtol(cp+1, &cpp, 10);
  1042. else
  1043. frame_rate->num = 0;
  1044. }
  1045. else {
  1046. /* Finally we give up and parse it as double */
  1047. AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
  1048. frame_rate->den = time_base.den;
  1049. frame_rate->num = time_base.num;
  1050. }
  1051. if (!frame_rate->num || !frame_rate->den)
  1052. return -1;
  1053. else
  1054. return 0;
  1055. }
  1056. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1057. int i;
  1058. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1059. return i;
  1060. }
  1061. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1062. {
  1063. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1064. "version to the newest one from SVN. If the problem still "
  1065. "occurs, it means that your file has a feature which has not "
  1066. "been implemented.", feature);
  1067. if(want_sample)
  1068. av_log_ask_for_sample(avc, NULL);
  1069. else
  1070. av_log(avc, AV_LOG_WARNING, "\n");
  1071. }
  1072. void av_log_ask_for_sample(void *avc, const char *msg)
  1073. {
  1074. if (msg)
  1075. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  1076. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1077. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1078. "and contact the ffmpeg-devel mailing list.\n");
  1079. }
  1080. static AVHWAccel *first_hwaccel = NULL;
  1081. void av_register_hwaccel(AVHWAccel *hwaccel)
  1082. {
  1083. AVHWAccel **p = &first_hwaccel;
  1084. while (*p)
  1085. p = &(*p)->next;
  1086. *p = hwaccel;
  1087. hwaccel->next = NULL;
  1088. }
  1089. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1090. {
  1091. return hwaccel ? hwaccel->next : first_hwaccel;
  1092. }
  1093. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1094. {
  1095. AVHWAccel *hwaccel=NULL;
  1096. while((hwaccel= av_hwaccel_next(hwaccel))){
  1097. if ( hwaccel->id == codec_id
  1098. && hwaccel->pix_fmt == pix_fmt)
  1099. return hwaccel;
  1100. }
  1101. return NULL;
  1102. }
  1103. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1104. {
  1105. if (ff_lockmgr_cb) {
  1106. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1107. return -1;
  1108. }
  1109. ff_lockmgr_cb = cb;
  1110. if (ff_lockmgr_cb) {
  1111. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1112. return -1;
  1113. }
  1114. return 0;
  1115. }
  1116. unsigned int ff_toupper4(unsigned int x)
  1117. {
  1118. return toupper( x &0xFF)
  1119. + (toupper((x>>8 )&0xFF)<<8 )
  1120. + (toupper((x>>16)&0xFF)<<16)
  1121. + (toupper((x>>24)&0xFF)<<24);
  1122. }