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.

393 lines
12KB

  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 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 "avformat.h"
  20. #define Y4M_MAGIC "YUV4MPEG2"
  21. #define Y4M_FRAME_MAGIC "FRAME"
  22. #define Y4M_LINE_MAX 256
  23. #ifdef CONFIG_ENCODERS
  24. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  25. {
  26. AVStream *st;
  27. int width, height;
  28. int raten, rated, aspectn, aspectd, n;
  29. char inter;
  30. char *colorspace = "";
  31. st = s->streams[0];
  32. width = st->codec.width;
  33. height = st->codec.height;
  34. av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
  35. aspectn = st->codec.sample_aspect_ratio.num;
  36. aspectd = st->codec.sample_aspect_ratio.den;
  37. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  38. inter = 'p'; /* progressive is the default */
  39. if (st->codec.coded_frame && st->codec.coded_frame->interlaced_frame) {
  40. inter = st->codec.coded_frame->top_field_first ? 't' : 'b';
  41. }
  42. switch(st->codec.pix_fmt) {
  43. case PIX_FMT_YUV411P:
  44. colorspace = " C411 XYSCSS=411";
  45. break;
  46. case PIX_FMT_YUV420P:
  47. colorspace = (st->codec.codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
  48. break;
  49. case PIX_FMT_YUV422P:
  50. colorspace = " C422 XYSCSS=422";
  51. break;
  52. case PIX_FMT_YUV444P:
  53. colorspace = " C444 XYSCSS=444";
  54. break;
  55. }
  56. /* construct stream header, if this is the first frame */
  57. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  58. Y4M_MAGIC,
  59. width,
  60. height,
  61. raten, rated,
  62. inter,
  63. aspectn, aspectd,
  64. colorspace);
  65. return n;
  66. }
  67. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  68. {
  69. AVStream *st = s->streams[pkt->stream_index];
  70. ByteIOContext *pb = &s->pb;
  71. AVPicture *picture;
  72. int* first_pkt = s->priv_data;
  73. int width, height, h_chroma_shift, v_chroma_shift;
  74. int i, m;
  75. char buf2[Y4M_LINE_MAX+1];
  76. char buf1[20];
  77. uint8_t *ptr, *ptr1, *ptr2;
  78. picture = (AVPicture *)pkt->data;
  79. /* for the first packet we have to output the header as well */
  80. if (*first_pkt) {
  81. *first_pkt = 0;
  82. if (yuv4_generate_header(s, buf2) < 0) {
  83. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  84. return AVERROR_IO;
  85. } else {
  86. put_buffer(pb, buf2, strlen(buf2));
  87. }
  88. }
  89. /* construct frame header */
  90. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  91. put_buffer(pb, buf1, strlen(buf1));
  92. width = st->codec.width;
  93. height = st->codec.height;
  94. ptr = picture->data[0];
  95. for(i=0;i<height;i++) {
  96. put_buffer(pb, ptr, width);
  97. ptr += picture->linesize[0];
  98. }
  99. // Adjust for smaller Cb and Cr planes
  100. avcodec_get_chroma_sub_sample(st->codec.pix_fmt, &h_chroma_shift, &v_chroma_shift);
  101. width >>= h_chroma_shift;
  102. height >>= v_chroma_shift;
  103. ptr1 = picture->data[1];
  104. ptr2 = picture->data[2];
  105. for(i=0;i<height;i++) { /* Cb */
  106. put_buffer(pb, ptr1, width);
  107. ptr1 += picture->linesize[1];
  108. }
  109. for(i=0;i<height;i++) { /* Cr */
  110. put_buffer(pb, ptr2, width);
  111. ptr2 += picture->linesize[2];
  112. }
  113. put_flush_packet(pb);
  114. return 0;
  115. }
  116. static int yuv4_write_header(AVFormatContext *s)
  117. {
  118. int* first_pkt = s->priv_data;
  119. if (s->nb_streams != 1)
  120. return AVERROR_IO;
  121. if (s->streams[0]->codec.pix_fmt == PIX_FMT_YUV411P) {
  122. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  123. }
  124. else if ((s->streams[0]->codec.pix_fmt != PIX_FMT_YUV420P) &&
  125. (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV422P) &&
  126. (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV444P)) {
  127. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles 4:4:4, 4:2:2, 4:2:0 and 4:1:1 planar YUV data. Use -pix_fmt to select one.\n");
  128. return AVERROR_IO;
  129. }
  130. *first_pkt = 1;
  131. return 0;
  132. }
  133. static int yuv4_write_trailer(AVFormatContext *s)
  134. {
  135. return 0;
  136. }
  137. AVOutputFormat yuv4mpegpipe_oformat = {
  138. "yuv4mpegpipe",
  139. "YUV4MPEG pipe format",
  140. "",
  141. "yuv4mpeg",
  142. sizeof(int),
  143. CODEC_ID_NONE,
  144. CODEC_ID_RAWVIDEO,
  145. yuv4_write_header,
  146. yuv4_write_packet,
  147. yuv4_write_trailer,
  148. .flags = AVFMT_RAWPICTURE,
  149. };
  150. #endif //CONFIG_ENCODERS
  151. /* Header size increased to allow room for optional flags */
  152. #define MAX_YUV4_HEADER 80
  153. #define MAX_FRAME_HEADER 80
  154. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  155. {
  156. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  157. char *tokstart,*tokend,*header_end;
  158. int i;
  159. ByteIOContext *pb = &s->pb;
  160. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0,interlaced_frame=0,top_field_first=0;
  161. enum PixelFormat pix_fmt=PIX_FMT_NB,alt_pix_fmt=PIX_FMT_NB;
  162. AVStream *st;
  163. for (i=0; i<MAX_YUV4_HEADER; i++) {
  164. header[i] = get_byte(pb);
  165. if (header[i] == '\n') {
  166. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  167. header[i+2] = 0;
  168. break;
  169. }
  170. }
  171. if (i == MAX_YUV4_HEADER) return -1;
  172. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  173. header_end = &header[i+1]; // Include space
  174. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  175. if (*tokstart==0x20) continue;
  176. switch (*tokstart++) {
  177. case 'W': // Width. Required.
  178. width = strtol(tokstart, &tokend, 10);
  179. tokstart=tokend;
  180. break;
  181. case 'H': // Height. Required.
  182. height = strtol(tokstart, &tokend, 10);
  183. tokstart=tokend;
  184. break;
  185. case 'C': // Color space
  186. if (strncmp("420jpeg",tokstart,7)==0)
  187. pix_fmt = PIX_FMT_YUV420P;
  188. else if (strncmp("420mpeg2",tokstart,8)==0)
  189. pix_fmt = PIX_FMT_YUV420P;
  190. else if (strncmp("420paldv", tokstart, 8)==0)
  191. pix_fmt = PIX_FMT_YUV420P;
  192. else if (strncmp("411", tokstart, 3)==0)
  193. pix_fmt = PIX_FMT_YUV411P;
  194. else if (strncmp("422", tokstart, 3)==0)
  195. pix_fmt = PIX_FMT_YUV422P;
  196. else if (strncmp("444alpha", tokstart, 8)==0) {
  197. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  198. return -1;
  199. } else if (strncmp("444", tokstart, 3)==0)
  200. pix_fmt = PIX_FMT_YUV444P;
  201. else if (strncmp("mono",tokstart, 4)==0) {
  202. av_log(s, AV_LOG_ERROR, "Cannot handle luma only YUV4MPEG stream.\n");
  203. return -1;
  204. } else {
  205. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  206. return -1;
  207. }
  208. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  209. break;
  210. case 'I': // Interlace type
  211. switch (*tokstart++){
  212. case '?':
  213. break;
  214. case 'p':
  215. interlaced_frame=0;
  216. break;
  217. case 't':
  218. interlaced_frame=1;
  219. top_field_first=1;
  220. break;
  221. case 'b':
  222. interlaced_frame=1;
  223. top_field_first=0;
  224. break;
  225. case 'm':
  226. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  227. return -1;
  228. default:
  229. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  230. return -1;
  231. }
  232. break;
  233. case 'F': // Frame rate
  234. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  235. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  236. break;
  237. case 'A': // Pixel aspect
  238. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  239. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  240. break;
  241. case 'X': // Vendor extensions
  242. if (strncmp("YSCSS=",tokstart,6)==0) {
  243. // Older nonstandard pixel format representation
  244. tokstart+=6;
  245. if (strncmp("420JPEG",tokstart,7)==0)
  246. alt_pix_fmt=PIX_FMT_YUV420P;
  247. else if (strncmp("420MPEG2",tokstart,8)==0)
  248. alt_pix_fmt=PIX_FMT_YUV420P;
  249. else if (strncmp("420PALDV",tokstart,8)==0)
  250. alt_pix_fmt=PIX_FMT_YUV420P;
  251. else if (strncmp("411",tokstart,3)==0)
  252. alt_pix_fmt=PIX_FMT_YUV411P;
  253. else if (strncmp("422",tokstart,3)==0)
  254. alt_pix_fmt=PIX_FMT_YUV422P;
  255. else if (strncmp("444",tokstart,3)==0)
  256. alt_pix_fmt=PIX_FMT_YUV444P;
  257. }
  258. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  259. break;
  260. }
  261. }
  262. if ((width == -1) || (height == -1)) {
  263. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  264. return -1;
  265. }
  266. if (pix_fmt == PIX_FMT_NB) {
  267. if (alt_pix_fmt == PIX_FMT_NB)
  268. pix_fmt = PIX_FMT_YUV420P;
  269. else
  270. pix_fmt = alt_pix_fmt;
  271. }
  272. if (raten == 0 && rated == 0) {
  273. // Frame rate unknown
  274. raten = 25;
  275. rated = 1;
  276. }
  277. if (aspectn == 0 && aspectd == 0) {
  278. // Pixel aspect unknown
  279. aspectd = 1;
  280. }
  281. st = av_new_stream(s, 0);
  282. st = s->streams[0];
  283. st->codec.width = width;
  284. st->codec.height = height;
  285. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  286. st->codec.frame_rate = raten;
  287. st->codec.frame_rate_base = rated;
  288. st->codec.pix_fmt = pix_fmt;
  289. st->codec.codec_type = CODEC_TYPE_VIDEO;
  290. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  291. st->codec.sample_aspect_ratio= (AVRational){aspectn, aspectd};
  292. return 0;
  293. }
  294. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  295. {
  296. int i;
  297. char header[MAX_FRAME_HEADER+1];
  298. int packet_size, ret, width, height;
  299. AVStream *st = s->streams[0];
  300. for (i=0; i<MAX_FRAME_HEADER; i++) {
  301. header[i] = get_byte(&s->pb);
  302. if (header[i] == '\n') {
  303. header[i+1] = 0;
  304. break;
  305. }
  306. }
  307. if (i == MAX_FRAME_HEADER) return -1;
  308. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  309. width = st->codec.width;
  310. height = st->codec.height;
  311. packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
  312. if (packet_size < 0)
  313. return -1;
  314. if (av_new_packet(pkt, packet_size) < 0)
  315. return AVERROR_IO;
  316. pkt->stream_index = 0;
  317. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  318. if (ret != pkt->size) {
  319. av_free_packet(pkt);
  320. return AVERROR_IO;
  321. } else {
  322. return 0;
  323. }
  324. }
  325. static int yuv4_read_close(AVFormatContext *s)
  326. {
  327. return 0;
  328. }
  329. AVInputFormat yuv4mpegpipe_iformat = {
  330. "yuv4mpegpipe",
  331. "YUV4MPEG pipe format",
  332. 0,
  333. NULL,
  334. yuv4_read_header,
  335. yuv4_read_packet,
  336. yuv4_read_close,
  337. .extensions = "yuv4mpeg"
  338. };
  339. int yuv4mpeg_init(void)
  340. {
  341. av_register_input_format(&yuv4mpegpipe_iformat);
  342. #ifdef CONFIG_ENCODERS
  343. av_register_output_format(&yuv4mpegpipe_oformat);
  344. #endif //CONFIG_ENCODERS
  345. return 0;
  346. }