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.

196 lines
5.7KB

  1. /*
  2. * Raw Video Codec
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file raw.c
  21. * Raw Video Codec
  22. */
  23. #include "avcodec.h"
  24. typedef struct RawVideoContext {
  25. unsigned char * buffer; /* block of memory for holding one frame */
  26. int length; /* number of bytes in buffer */
  27. AVFrame pic; ///< AVCodecContext.coded_frame
  28. } RawVideoContext;
  29. typedef struct PixelFormatTag {
  30. int pix_fmt;
  31. unsigned int fourcc;
  32. } PixelFormatTag;
  33. const PixelFormatTag pixelFormatTags[] = {
  34. { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') }, /* Planar formats */
  35. { PIX_FMT_YUV420P, MKTAG('I', 'Y', 'U', 'V') },
  36. { PIX_FMT_YUV410P, MKTAG('Y', 'U', 'V', '9') },
  37. { PIX_FMT_YUV411P, MKTAG('Y', '4', '1', 'B') },
  38. { PIX_FMT_YUV422P, MKTAG('Y', '4', '2', 'B') },
  39. { PIX_FMT_GRAY8, MKTAG('Y', '8', '0', '0') },
  40. { PIX_FMT_GRAY8, MKTAG(' ', ' ', 'Y', '8') },
  41. { PIX_FMT_YUV422, MKTAG('Y', 'U', 'Y', '2') }, /* Packed formats */
  42. { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') },
  43. { PIX_FMT_UYVY422, MKTAG('U', 'Y', 'V', 'Y') },
  44. { PIX_FMT_GRAY8, MKTAG('G', 'R', 'E', 'Y') },
  45. /* quicktime */
  46. { PIX_FMT_UYVY422, MKTAG('2', 'v', 'u', 'y') },
  47. { -1, 0 },
  48. };
  49. static int findPixelFormat(unsigned int fourcc)
  50. {
  51. const PixelFormatTag * tags = pixelFormatTags;
  52. while (tags->pix_fmt >= 0) {
  53. if (tags->fourcc == fourcc)
  54. return tags->pix_fmt;
  55. tags++;
  56. }
  57. return PIX_FMT_YUV420P;
  58. }
  59. unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat fmt)
  60. {
  61. const PixelFormatTag * tags = pixelFormatTags;
  62. while (tags->pix_fmt >= 0) {
  63. if (tags->pix_fmt == fmt)
  64. return tags->fourcc;
  65. tags++;
  66. }
  67. return 0;
  68. }
  69. /* RAW Decoder Implementation */
  70. static int raw_init_decoder(AVCodecContext *avctx)
  71. {
  72. RawVideoContext *context = avctx->priv_data;
  73. if (avctx->codec_tag)
  74. avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
  75. else if (avctx->bits_per_sample){
  76. switch(avctx->bits_per_sample){
  77. case 8: avctx->pix_fmt= PIX_FMT_PAL8 ; break;
  78. case 15: avctx->pix_fmt= PIX_FMT_RGB555; break;
  79. case 16: avctx->pix_fmt= PIX_FMT_RGB565; break;
  80. case 24: avctx->pix_fmt= PIX_FMT_BGR24 ; break;
  81. case 32: avctx->pix_fmt= PIX_FMT_RGBA32; break;
  82. }
  83. }
  84. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  85. context->buffer = av_malloc(context->length);
  86. context->pic.pict_type = FF_I_TYPE;
  87. context->pic.key_frame = 1;
  88. avctx->coded_frame= &context->pic;
  89. if (!context->buffer)
  90. return -1;
  91. return 0;
  92. }
  93. static void flip(AVCodecContext *avctx, AVPicture * picture){
  94. if(!avctx->codec_tag && avctx->bits_per_sample && picture->linesize[2]==0){
  95. picture->data[0] += picture->linesize[0] * (avctx->height-1);
  96. picture->linesize[0] *= -1;
  97. }
  98. }
  99. static int raw_decode(AVCodecContext *avctx,
  100. void *data, int *data_size,
  101. uint8_t *buf, int buf_size)
  102. {
  103. RawVideoContext *context = avctx->priv_data;
  104. AVFrame * frame = (AVFrame *) data;
  105. AVPicture * picture = (AVPicture *) data;
  106. frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
  107. frame->top_field_first = avctx->coded_frame->top_field_first;
  108. if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
  109. return -1;
  110. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  111. if(avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length){
  112. frame->data[1]= context->buffer;
  113. }
  114. if (avctx->palctrl && avctx->palctrl->palette_changed) {
  115. memcpy(frame->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  116. avctx->palctrl->palette_changed = 0;
  117. }
  118. flip(avctx, picture);
  119. *data_size = sizeof(AVPicture);
  120. return buf_size;
  121. }
  122. static int raw_close_decoder(AVCodecContext *avctx)
  123. {
  124. RawVideoContext *context = avctx->priv_data;
  125. av_freep(&context->buffer);
  126. return 0;
  127. }
  128. /* RAW Encoder Implementation */
  129. static int raw_init_encoder(AVCodecContext *avctx)
  130. {
  131. avctx->coded_frame = (AVFrame *)avctx->priv_data;
  132. avctx->coded_frame->pict_type = FF_I_TYPE;
  133. avctx->coded_frame->key_frame = 1;
  134. if(!avctx->codec_tag)
  135. avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
  136. return 0;
  137. }
  138. static int raw_encode(AVCodecContext *avctx,
  139. unsigned char *frame, int buf_size, void *data)
  140. {
  141. return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
  142. avctx->height, frame, buf_size);
  143. }
  144. #ifdef CONFIG_RAWVIDEO_ENCODER
  145. AVCodec rawvideo_encoder = {
  146. "rawvideo",
  147. CODEC_TYPE_VIDEO,
  148. CODEC_ID_RAWVIDEO,
  149. sizeof(AVFrame),
  150. raw_init_encoder,
  151. raw_encode,
  152. };
  153. #endif // CONFIG_RAWVIDEO_ENCODER
  154. AVCodec rawvideo_decoder = {
  155. "rawvideo",
  156. CODEC_TYPE_VIDEO,
  157. CODEC_ID_RAWVIDEO,
  158. sizeof(RawVideoContext),
  159. raw_init_decoder,
  160. NULL,
  161. raw_close_decoder,
  162. raw_decode,
  163. };