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.

715 lines
18KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(unsigned int size)
  23. {
  24. void *ptr;
  25. if(size == 0) fprintf(stderr, "Warning, allocating 0 bytes\n");
  26. ptr = av_malloc(size);
  27. if (!ptr)
  28. return NULL;
  29. memset(ptr, 0, size);
  30. return ptr;
  31. }
  32. /* allocation of static arrays - do not use for normal allocation */
  33. static unsigned int last_static = 0;
  34. static char*** array_static = NULL;
  35. static const unsigned int grow_static = 64; // ^2
  36. void *__av_mallocz_static(void** location, unsigned int size)
  37. {
  38. int l = (last_static + grow_static) & ~(grow_static - 1);
  39. void *ptr = av_mallocz(size);
  40. if (!ptr)
  41. return NULL;
  42. if (location)
  43. {
  44. if (l > last_static)
  45. array_static = realloc(array_static, l);
  46. array_static[last_static++] = (char**) location;
  47. *location = ptr;
  48. }
  49. return ptr;
  50. }
  51. /* free all static arrays and reset pointers to 0 */
  52. void av_free_static()
  53. {
  54. if (array_static)
  55. {
  56. unsigned i;
  57. for (i = 0; i < last_static; i++)
  58. {
  59. free(*array_static[i]);
  60. *array_static[i] = NULL;
  61. }
  62. free(array_static);
  63. array_static = 0;
  64. }
  65. last_static = 0;
  66. }
  67. /* cannot call it directly because of 'void **' casting is not automatic */
  68. void __av_freep(void **ptr)
  69. {
  70. av_free(*ptr);
  71. *ptr = NULL;
  72. }
  73. /* encoder management */
  74. AVCodec *first_avcodec;
  75. void register_avcodec(AVCodec *format)
  76. {
  77. AVCodec **p;
  78. p = &first_avcodec;
  79. while (*p != NULL) p = &(*p)->next;
  80. *p = format;
  81. format->next = NULL;
  82. }
  83. void avcodec_get_chroma_sub_sample(int fmt, int *h_shift, int *v_shift){
  84. switch(fmt){
  85. case PIX_FMT_YUV410P:
  86. *h_shift=2;
  87. *v_shift=2;
  88. break;
  89. case PIX_FMT_YUV420P:
  90. *h_shift=1;
  91. *v_shift=1;
  92. break;
  93. case PIX_FMT_YUV411P:
  94. *h_shift=2;
  95. *v_shift=0;
  96. break;
  97. case PIX_FMT_YUV422P:
  98. case PIX_FMT_YUV422:
  99. *h_shift=1;
  100. *v_shift=0;
  101. break;
  102. default: //RGB/...
  103. *h_shift=0;
  104. *v_shift=0;
  105. break;
  106. }
  107. }
  108. typedef struct DefaultPicOpaque{
  109. int last_pic_num;
  110. uint8_t *data[4];
  111. }DefaultPicOpaque;
  112. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  113. int i;
  114. const int width = s->width;
  115. const int height= s->height;
  116. DefaultPicOpaque *opaque;
  117. assert(pic->data[0]==NULL);
  118. assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL);
  119. if(pic->opaque){
  120. opaque= (DefaultPicOpaque *)pic->opaque;
  121. for(i=0; i<3; i++)
  122. pic->data[i]= opaque->data[i];
  123. // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
  124. pic->age= pic->coded_picture_number - opaque->last_pic_num;
  125. opaque->last_pic_num= pic->coded_picture_number;
  126. //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
  127. }else{
  128. int align, h_chroma_shift, v_chroma_shift;
  129. int w, h, pixel_size;
  130. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  131. switch(s->pix_fmt){
  132. case PIX_FMT_YUV422:
  133. pixel_size=2;
  134. break;
  135. case PIX_FMT_RGB24:
  136. case PIX_FMT_BGR24:
  137. pixel_size=3;
  138. break;
  139. case PIX_FMT_BGRA32:
  140. case PIX_FMT_RGBA32:
  141. pixel_size=4;
  142. break;
  143. default:
  144. pixel_size=1;
  145. }
  146. if(s->codec_id==CODEC_ID_SVQ1) align=63;
  147. else align=15;
  148. w= (width +align)&~align;
  149. h= (height+align)&~align;
  150. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  151. w+= EDGE_WIDTH*2;
  152. h+= EDGE_WIDTH*2;
  153. }
  154. opaque= av_mallocz(sizeof(DefaultPicOpaque));
  155. if(opaque==NULL) return -1;
  156. pic->opaque= opaque;
  157. opaque->last_pic_num= -256*256*256*64;
  158. for(i=0; i<3; i++){
  159. int h_shift= i==0 ? 0 : h_chroma_shift;
  160. int v_shift= i==0 ? 0 : v_chroma_shift;
  161. pic->linesize[i]= pixel_size*w>>h_shift;
  162. pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  163. if(pic->base[i]==NULL) return -1;
  164. memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
  165. if(s->flags&CODEC_FLAG_EMU_EDGE)
  166. pic->data[i] = pic->base[i] + 16; //FIXME 16
  167. else
  168. pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
  169. opaque->data[i]= pic->data[i];
  170. }
  171. pic->age= 256*256*256*64;
  172. pic->type= FF_BUFFER_TYPE_INTERNAL;
  173. }
  174. return 0;
  175. }
  176. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  177. int i;
  178. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  179. for(i=0; i<3; i++)
  180. pic->data[i]=NULL;
  181. //printf("R%X\n", pic->opaque);
  182. }
  183. void avcodec_get_context_defaults(AVCodecContext *s){
  184. s->bit_rate= 800*1000;
  185. s->bit_rate_tolerance= s->bit_rate*10;
  186. s->qmin= 2;
  187. s->qmax= 31;
  188. s->mb_qmin= 2;
  189. s->mb_qmax= 31;
  190. s->rc_eq= "tex^qComp";
  191. s->qcompress= 0.5;
  192. s->max_qdiff= 3;
  193. s->b_quant_factor=1.25;
  194. s->b_quant_offset=1.25;
  195. s->i_quant_factor=-0.8;
  196. s->i_quant_offset=0.0;
  197. s->error_concealment= 3;
  198. s->error_resilience= 1;
  199. s->workaround_bugs= FF_BUG_AUTODETECT;
  200. s->frame_rate = 25 * FRAME_RATE_BASE;
  201. s->gop_size= 50;
  202. s->me_method= ME_EPZS;
  203. s->get_buffer= avcodec_default_get_buffer;
  204. s->release_buffer= avcodec_default_release_buffer;
  205. }
  206. /**
  207. * allocates a AVCodecContext and set it to defaults.
  208. * this can be deallocated by simply calling free()
  209. */
  210. AVCodecContext *avcodec_alloc_context(void){
  211. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  212. if(avctx==NULL) return NULL;
  213. avcodec_get_context_defaults(avctx);
  214. return avctx;
  215. }
  216. /**
  217. * allocates a AVPFrame and set it to defaults.
  218. * this can be deallocated by simply calling free()
  219. */
  220. AVFrame *avcodec_alloc_frame(void){
  221. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  222. return pic;
  223. }
  224. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  225. {
  226. int ret;
  227. avctx->codec = codec;
  228. avctx->codec_id = codec->id;
  229. avctx->frame_number = 0;
  230. if (codec->priv_data_size > 0) {
  231. avctx->priv_data = av_mallocz(codec->priv_data_size);
  232. if (!avctx->priv_data)
  233. return -ENOMEM;
  234. } else {
  235. avctx->priv_data = NULL;
  236. }
  237. ret = avctx->codec->init(avctx);
  238. if (ret < 0) {
  239. av_freep(&avctx->priv_data);
  240. return ret;
  241. }
  242. return 0;
  243. }
  244. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  245. const short *samples)
  246. {
  247. int ret;
  248. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  249. avctx->frame_number++;
  250. return ret;
  251. }
  252. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  253. const AVFrame *pict)
  254. {
  255. int ret;
  256. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  257. emms_c(); //needed to avoid a emms_c() call before every return;
  258. avctx->frame_number++;
  259. return ret;
  260. }
  261. /* decode a frame. return -1 if error, otherwise return the number of
  262. bytes used. If no frame could be decompressed, *got_picture_ptr is
  263. zero. Otherwise, it is non zero */
  264. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  265. int *got_picture_ptr,
  266. UINT8 *buf, int buf_size)
  267. {
  268. int ret;
  269. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  270. buf, buf_size);
  271. emms_c(); //needed to avoid a emms_c() call before every return;
  272. if (*got_picture_ptr)
  273. avctx->frame_number++;
  274. return ret;
  275. }
  276. /* decode an audio frame. return -1 if error, otherwise return the
  277. *number of bytes used. If no frame could be decompressed,
  278. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  279. *size in BYTES. */
  280. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  281. int *frame_size_ptr,
  282. UINT8 *buf, int buf_size)
  283. {
  284. int ret;
  285. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  286. buf, buf_size);
  287. avctx->frame_number++;
  288. return ret;
  289. }
  290. int avcodec_close(AVCodecContext *avctx)
  291. {
  292. if (avctx->codec->close)
  293. avctx->codec->close(avctx);
  294. av_freep(&avctx->priv_data);
  295. avctx->codec = NULL;
  296. return 0;
  297. }
  298. AVCodec *avcodec_find_encoder(enum CodecID id)
  299. {
  300. AVCodec *p;
  301. p = first_avcodec;
  302. while (p) {
  303. if (p->encode != NULL && p->id == id)
  304. return p;
  305. p = p->next;
  306. }
  307. return NULL;
  308. }
  309. AVCodec *avcodec_find_encoder_by_name(const char *name)
  310. {
  311. AVCodec *p;
  312. p = first_avcodec;
  313. while (p) {
  314. if (p->encode != NULL && strcmp(name,p->name) == 0)
  315. return p;
  316. p = p->next;
  317. }
  318. return NULL;
  319. }
  320. AVCodec *avcodec_find_decoder(enum CodecID id)
  321. {
  322. AVCodec *p;
  323. p = first_avcodec;
  324. while (p) {
  325. if (p->decode != NULL && p->id == id)
  326. return p;
  327. p = p->next;
  328. }
  329. return NULL;
  330. }
  331. AVCodec *avcodec_find_decoder_by_name(const char *name)
  332. {
  333. AVCodec *p;
  334. p = first_avcodec;
  335. while (p) {
  336. if (p->decode != NULL && strcmp(name,p->name) == 0)
  337. return p;
  338. p = p->next;
  339. }
  340. return NULL;
  341. }
  342. AVCodec *avcodec_find(enum CodecID id)
  343. {
  344. AVCodec *p;
  345. p = first_avcodec;
  346. while (p) {
  347. if (p->id == id)
  348. return p;
  349. p = p->next;
  350. }
  351. return NULL;
  352. }
  353. const char *pix_fmt_str[] = {
  354. "yuv420p",
  355. "yuv422",
  356. "rgb24",
  357. "bgr24",
  358. "yuv422p",
  359. "yuv444p",
  360. "rgba32",
  361. "bgra32",
  362. "yuv410p",
  363. "yuv411p",
  364. };
  365. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  366. {
  367. const char *codec_name;
  368. AVCodec *p;
  369. char buf1[32];
  370. char channels_str[100];
  371. int bitrate;
  372. if (encode)
  373. p = avcodec_find_encoder(enc->codec_id);
  374. else
  375. p = avcodec_find_decoder(enc->codec_id);
  376. if (p) {
  377. codec_name = p->name;
  378. } else if (enc->codec_name[0] != '\0') {
  379. codec_name = enc->codec_name;
  380. } else {
  381. /* output avi tags */
  382. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  383. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  384. enc->codec_tag & 0xff,
  385. (enc->codec_tag >> 8) & 0xff,
  386. (enc->codec_tag >> 16) & 0xff,
  387. (enc->codec_tag >> 24) & 0xff);
  388. } else {
  389. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  390. }
  391. codec_name = buf1;
  392. }
  393. switch(enc->codec_type) {
  394. case CODEC_TYPE_VIDEO:
  395. snprintf(buf, buf_size,
  396. "Video: %s%s",
  397. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  398. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  399. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  400. ", %s",
  401. pix_fmt_str[enc->pix_fmt]);
  402. }
  403. if (enc->width) {
  404. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  405. ", %dx%d, %0.2f fps",
  406. enc->width, enc->height,
  407. (float)enc->frame_rate / FRAME_RATE_BASE);
  408. }
  409. if (encode) {
  410. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  411. ", q=%d-%d", enc->qmin, enc->qmax);
  412. }
  413. bitrate = enc->bit_rate;
  414. break;
  415. case CODEC_TYPE_AUDIO:
  416. snprintf(buf, buf_size,
  417. "Audio: %s",
  418. codec_name);
  419. switch (enc->channels) {
  420. case 1:
  421. strcpy(channels_str, "mono");
  422. break;
  423. case 2:
  424. strcpy(channels_str, "stereo");
  425. break;
  426. case 6:
  427. strcpy(channels_str, "5:1");
  428. break;
  429. default:
  430. sprintf(channels_str, "%d channels", enc->channels);
  431. break;
  432. }
  433. if (enc->sample_rate) {
  434. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  435. ", %d Hz, %s",
  436. enc->sample_rate,
  437. channels_str);
  438. }
  439. /* for PCM codecs, compute bitrate directly */
  440. switch(enc->codec_id) {
  441. case CODEC_ID_PCM_S16LE:
  442. case CODEC_ID_PCM_S16BE:
  443. case CODEC_ID_PCM_U16LE:
  444. case CODEC_ID_PCM_U16BE:
  445. bitrate = enc->sample_rate * enc->channels * 16;
  446. break;
  447. case CODEC_ID_PCM_S8:
  448. case CODEC_ID_PCM_U8:
  449. case CODEC_ID_PCM_ALAW:
  450. case CODEC_ID_PCM_MULAW:
  451. bitrate = enc->sample_rate * enc->channels * 8;
  452. break;
  453. default:
  454. bitrate = enc->bit_rate;
  455. break;
  456. }
  457. break;
  458. default:
  459. av_abort();
  460. }
  461. if (encode) {
  462. if (enc->flags & CODEC_FLAG_PASS1)
  463. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  464. ", pass 1");
  465. if (enc->flags & CODEC_FLAG_PASS2)
  466. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  467. ", pass 2");
  468. }
  469. if (bitrate != 0) {
  470. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  471. ", %d kb/s", bitrate / 1000);
  472. }
  473. }
  474. /* Picture field are filled with 'ptr' addresses */
  475. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  476. int pix_fmt, int width, int height)
  477. {
  478. int size;
  479. size = width * height;
  480. switch(pix_fmt) {
  481. case PIX_FMT_YUV420P:
  482. picture->data[0] = ptr;
  483. picture->data[1] = picture->data[0] + size;
  484. picture->data[2] = picture->data[1] + size / 4;
  485. picture->linesize[0] = width;
  486. picture->linesize[1] = width / 2;
  487. picture->linesize[2] = width / 2;
  488. break;
  489. case PIX_FMT_YUV422P:
  490. picture->data[0] = ptr;
  491. picture->data[1] = picture->data[0] + size;
  492. picture->data[2] = picture->data[1] + size / 2;
  493. picture->linesize[0] = width;
  494. picture->linesize[1] = width / 2;
  495. picture->linesize[2] = width / 2;
  496. break;
  497. case PIX_FMT_YUV444P:
  498. picture->data[0] = ptr;
  499. picture->data[1] = picture->data[0] + size;
  500. picture->data[2] = picture->data[1] + size;
  501. picture->linesize[0] = width;
  502. picture->linesize[1] = width;
  503. picture->linesize[2] = width;
  504. break;
  505. case PIX_FMT_RGB24:
  506. case PIX_FMT_BGR24:
  507. picture->data[0] = ptr;
  508. picture->data[1] = NULL;
  509. picture->data[2] = NULL;
  510. picture->linesize[0] = width * 3;
  511. break;
  512. case PIX_FMT_RGBA32:
  513. case PIX_FMT_BGRA32:
  514. picture->data[0] = ptr;
  515. picture->data[1] = NULL;
  516. picture->data[2] = NULL;
  517. picture->linesize[0] = width * 4;
  518. break;
  519. case PIX_FMT_YUV422:
  520. picture->data[0] = ptr;
  521. picture->data[1] = NULL;
  522. picture->data[2] = NULL;
  523. picture->linesize[0] = width * 2;
  524. break;
  525. default:
  526. picture->data[0] = NULL;
  527. picture->data[1] = NULL;
  528. picture->data[2] = NULL;
  529. break;
  530. }
  531. }
  532. int avpicture_get_size(int pix_fmt, int width, int height)
  533. {
  534. int size;
  535. size = width * height;
  536. switch(pix_fmt) {
  537. case PIX_FMT_YUV420P:
  538. size = (size * 3) / 2;
  539. break;
  540. case PIX_FMT_YUV422P:
  541. size = (size * 2);
  542. break;
  543. case PIX_FMT_YUV444P:
  544. size = (size * 3);
  545. break;
  546. case PIX_FMT_RGB24:
  547. case PIX_FMT_BGR24:
  548. size = (size * 3);
  549. break;
  550. case PIX_FMT_RGBA32:
  551. case PIX_FMT_BGRA32:
  552. size = (size * 4);
  553. break;
  554. case PIX_FMT_YUV422:
  555. size = (size * 2);
  556. break;
  557. default:
  558. size = -1;
  559. break;
  560. }
  561. return size;
  562. }
  563. unsigned avcodec_version( void )
  564. {
  565. return LIBAVCODEC_VERSION_INT;
  566. }
  567. unsigned avcodec_build( void )
  568. {
  569. return LIBAVCODEC_BUILD;
  570. }
  571. /* must be called before any other functions */
  572. void avcodec_init(void)
  573. {
  574. static int inited = 0;
  575. if (inited != 0)
  576. return;
  577. inited = 1;
  578. //dsputil_init();
  579. }
  580. /* this can be called after seeking and before trying to decode the next keyframe */
  581. void avcodec_flush_buffers(AVCodecContext *avctx)
  582. {
  583. int i;
  584. MpegEncContext *s = avctx->priv_data;
  585. switch(avctx->codec_id){
  586. case CODEC_ID_MPEG1VIDEO:
  587. case CODEC_ID_H263:
  588. case CODEC_ID_RV10:
  589. case CODEC_ID_MJPEG:
  590. case CODEC_ID_MJPEGB:
  591. case CODEC_ID_MPEG4:
  592. case CODEC_ID_MSMPEG4V1:
  593. case CODEC_ID_MSMPEG4V2:
  594. case CODEC_ID_MSMPEG4V3:
  595. case CODEC_ID_WMV1:
  596. case CODEC_ID_WMV2:
  597. case CODEC_ID_H263P:
  598. case CODEC_ID_H263I:
  599. case CODEC_ID_SVQ1:
  600. for(i=0; i<MAX_PICTURE_COUNT; i++){
  601. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  602. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  603. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  604. }
  605. break;
  606. default:
  607. //FIXME
  608. break;
  609. }
  610. }
  611. static int raw_encode_init(AVCodecContext *s)
  612. {
  613. return 0;
  614. }
  615. static int raw_decode_frame(AVCodecContext *avctx,
  616. void *data, int *data_size,
  617. UINT8 *buf, int buf_size)
  618. {
  619. return -1;
  620. }
  621. static int raw_encode_frame(AVCodecContext *avctx,
  622. unsigned char *frame, int buf_size, void *data)
  623. {
  624. return -1;
  625. }
  626. AVCodec rawvideo_codec = {
  627. "rawvideo",
  628. CODEC_TYPE_VIDEO,
  629. CODEC_ID_RAWVIDEO,
  630. 0,
  631. raw_encode_init,
  632. raw_encode_frame,
  633. NULL,
  634. raw_decode_frame,
  635. };