yuv4mpeg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #define Y4M_MAGIC "YUV4MPEG2"
  23. #define Y4M_FRAME_MAGIC "FRAME"
  24. #define Y4M_LINE_MAX 256
  25. struct frame_attributes {
  26. int interlaced_frame;
  27. int top_field_first;
  28. };
  29. #if CONFIG_YUV4MPEGPIPE_MUXER
  30. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  31. {
  32. AVStream *st;
  33. int width, height;
  34. int raten, rated, aspectn, aspectd, n;
  35. char inter;
  36. const char *colorspace = "";
  37. st = s->streams[0];
  38. width = st->codec->width;
  39. height = st->codec->height;
  40. av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
  41. aspectn = st->sample_aspect_ratio.num;
  42. aspectd = st->sample_aspect_ratio.den;
  43. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  44. inter = 'p'; /* progressive is the default */
  45. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
  46. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  47. }
  48. switch(st->codec->pix_fmt) {
  49. case PIX_FMT_GRAY8:
  50. colorspace = " Cmono";
  51. break;
  52. case PIX_FMT_YUV411P:
  53. colorspace = " C411 XYSCSS=411";
  54. break;
  55. case PIX_FMT_YUV420P:
  56. colorspace = (st->codec->chroma_sample_location == AVCHROMA_LOC_TOPLEFT)?" C420paldv XYSCSS=420PALDV":
  57. (st->codec->chroma_sample_location == AVCHROMA_LOC_LEFT) ?" C420mpeg2 XYSCSS=420MPEG2":
  58. " C420jpeg XYSCSS=420JPEG";
  59. break;
  60. case PIX_FMT_YUV422P:
  61. colorspace = " C422 XYSCSS=422";
  62. break;
  63. case PIX_FMT_YUV444P:
  64. colorspace = " C444 XYSCSS=444";
  65. break;
  66. }
  67. /* construct stream header, if this is the first frame */
  68. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  69. Y4M_MAGIC,
  70. width,
  71. height,
  72. raten, rated,
  73. inter,
  74. aspectn, aspectd,
  75. colorspace);
  76. return n;
  77. }
  78. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  79. {
  80. AVStream *st = s->streams[pkt->stream_index];
  81. AVIOContext *pb = s->pb;
  82. AVPicture *picture;
  83. int* first_pkt = s->priv_data;
  84. int width, height, h_chroma_shift, v_chroma_shift;
  85. int i;
  86. char buf2[Y4M_LINE_MAX+1];
  87. char buf1[20];
  88. uint8_t *ptr, *ptr1, *ptr2;
  89. picture = (AVPicture *)pkt->data;
  90. /* for the first packet we have to output the header as well */
  91. if (*first_pkt) {
  92. *first_pkt = 0;
  93. if (yuv4_generate_header(s, buf2) < 0) {
  94. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  95. return AVERROR(EIO);
  96. } else {
  97. avio_write(pb, buf2, strlen(buf2));
  98. }
  99. }
  100. /* construct frame header */
  101. snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  102. avio_write(pb, buf1, strlen(buf1));
  103. width = st->codec->width;
  104. height = st->codec->height;
  105. ptr = picture->data[0];
  106. for(i=0;i<height;i++) {
  107. avio_write(pb, ptr, width);
  108. ptr += picture->linesize[0];
  109. }
  110. if (st->codec->pix_fmt != PIX_FMT_GRAY8){
  111. // Adjust for smaller Cb and Cr planes
  112. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  113. width >>= h_chroma_shift;
  114. height >>= v_chroma_shift;
  115. ptr1 = picture->data[1];
  116. ptr2 = picture->data[2];
  117. for(i=0;i<height;i++) { /* Cb */
  118. avio_write(pb, ptr1, width);
  119. ptr1 += picture->linesize[1];
  120. }
  121. for(i=0;i<height;i++) { /* Cr */
  122. avio_write(pb, ptr2, width);
  123. ptr2 += picture->linesize[2];
  124. }
  125. }
  126. avio_flush(pb);
  127. return 0;
  128. }
  129. static int yuv4_write_header(AVFormatContext *s)
  130. {
  131. int* first_pkt = s->priv_data;
  132. if (s->nb_streams != 1)
  133. return AVERROR(EIO);
  134. if (s->streams[0]->codec->codec_id != CODEC_ID_RAWVIDEO) {
  135. av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
  136. return AVERROR_INVALIDDATA;
  137. }
  138. if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
  139. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  140. }
  141. else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
  142. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  143. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  144. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
  145. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
  146. return AVERROR(EIO);
  147. }
  148. *first_pkt = 1;
  149. return 0;
  150. }
  151. AVOutputFormat ff_yuv4mpegpipe_muxer = {
  152. "yuv4mpegpipe",
  153. NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  154. "",
  155. "y4m",
  156. sizeof(int),
  157. CODEC_ID_NONE,
  158. CODEC_ID_RAWVIDEO,
  159. yuv4_write_header,
  160. yuv4_write_packet,
  161. .flags = AVFMT_RAWPICTURE,
  162. };
  163. #endif
  164. /* Header size increased to allow room for optional flags */
  165. #define MAX_YUV4_HEADER 80
  166. #define MAX_FRAME_HEADER 80
  167. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  168. {
  169. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  170. char *tokstart,*tokend,*header_end;
  171. int i;
  172. AVIOContext *pb = s->pb;
  173. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
  174. enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
  175. enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
  176. AVStream *st;
  177. struct frame_attributes *s1 = s->priv_data;
  178. for (i=0; i<MAX_YUV4_HEADER; i++) {
  179. header[i] = avio_r8(pb);
  180. if (header[i] == '\n') {
  181. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  182. header[i+2] = 0;
  183. break;
  184. }
  185. }
  186. if (i == MAX_YUV4_HEADER) return -1;
  187. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  188. s1->interlaced_frame = 0;
  189. s1->top_field_first = 0;
  190. header_end = &header[i+1]; // Include space
  191. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  192. if (*tokstart==0x20) continue;
  193. switch (*tokstart++) {
  194. case 'W': // Width. Required.
  195. width = strtol(tokstart, &tokend, 10);
  196. tokstart=tokend;
  197. break;
  198. case 'H': // Height. Required.
  199. height = strtol(tokstart, &tokend, 10);
  200. tokstart=tokend;
  201. break;
  202. case 'C': // Color space
  203. if (strncmp("420jpeg",tokstart,7)==0) {
  204. pix_fmt = PIX_FMT_YUV420P;
  205. chroma_sample_location = AVCHROMA_LOC_CENTER;
  206. } else if (strncmp("420mpeg2",tokstart,8)==0) {
  207. pix_fmt = PIX_FMT_YUV420P;
  208. chroma_sample_location = AVCHROMA_LOC_LEFT;
  209. } else if (strncmp("420paldv", tokstart, 8)==0) {
  210. pix_fmt = PIX_FMT_YUV420P;
  211. chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  212. } else if (strncmp("411", tokstart, 3)==0)
  213. pix_fmt = PIX_FMT_YUV411P;
  214. else if (strncmp("422", tokstart, 3)==0)
  215. pix_fmt = PIX_FMT_YUV422P;
  216. else if (strncmp("444alpha", tokstart, 8)==0) {
  217. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  218. return -1;
  219. } else if (strncmp("444", tokstart, 3)==0)
  220. pix_fmt = PIX_FMT_YUV444P;
  221. else if (strncmp("mono",tokstart, 4)==0) {
  222. pix_fmt = PIX_FMT_GRAY8;
  223. } else {
  224. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  225. return -1;
  226. }
  227. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  228. break;
  229. case 'I': // Interlace type
  230. switch (*tokstart++){
  231. case '?':
  232. break;
  233. case 'p':
  234. s1->interlaced_frame=0;
  235. break;
  236. case 't':
  237. s1->interlaced_frame=1;
  238. s1->top_field_first=1;
  239. break;
  240. case 'b':
  241. s1->interlaced_frame=1;
  242. s1->top_field_first=0;
  243. break;
  244. case 'm':
  245. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  246. return -1;
  247. default:
  248. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  249. return -1;
  250. }
  251. break;
  252. case 'F': // Frame rate
  253. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  254. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  255. break;
  256. case 'A': // Pixel aspect
  257. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  258. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  259. break;
  260. case 'X': // Vendor extensions
  261. if (strncmp("YSCSS=",tokstart,6)==0) {
  262. // Older nonstandard pixel format representation
  263. tokstart+=6;
  264. if (strncmp("420JPEG",tokstart,7)==0)
  265. alt_pix_fmt=PIX_FMT_YUV420P;
  266. else if (strncmp("420MPEG2",tokstart,8)==0)
  267. alt_pix_fmt=PIX_FMT_YUV420P;
  268. else if (strncmp("420PALDV",tokstart,8)==0)
  269. alt_pix_fmt=PIX_FMT_YUV420P;
  270. else if (strncmp("411",tokstart,3)==0)
  271. alt_pix_fmt=PIX_FMT_YUV411P;
  272. else if (strncmp("422",tokstart,3)==0)
  273. alt_pix_fmt=PIX_FMT_YUV422P;
  274. else if (strncmp("444",tokstart,3)==0)
  275. alt_pix_fmt=PIX_FMT_YUV444P;
  276. }
  277. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  278. break;
  279. }
  280. }
  281. if ((width == -1) || (height == -1)) {
  282. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  283. return -1;
  284. }
  285. if (pix_fmt == PIX_FMT_NONE) {
  286. if (alt_pix_fmt == PIX_FMT_NONE)
  287. pix_fmt = PIX_FMT_YUV420P;
  288. else
  289. pix_fmt = alt_pix_fmt;
  290. }
  291. if (raten <= 0 || rated <= 0) {
  292. // Frame rate unknown
  293. raten = 25;
  294. rated = 1;
  295. }
  296. if (aspectn == 0 && aspectd == 0) {
  297. // Pixel aspect unknown
  298. aspectd = 1;
  299. }
  300. st = av_new_stream(s, 0);
  301. if(!st)
  302. return AVERROR(ENOMEM);
  303. st->codec->width = width;
  304. st->codec->height = height;
  305. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  306. av_set_pts_info(st, 64, rated, raten);
  307. st->codec->pix_fmt = pix_fmt;
  308. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  309. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  310. st->sample_aspect_ratio= (AVRational){aspectn, aspectd};
  311. st->codec->chroma_sample_location = chroma_sample_location;
  312. return 0;
  313. }
  314. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  315. {
  316. int i;
  317. char header[MAX_FRAME_HEADER+1];
  318. int packet_size, width, height, ret;
  319. AVStream *st = s->streams[0];
  320. struct frame_attributes *s1 = s->priv_data;
  321. for (i=0; i<MAX_FRAME_HEADER; i++) {
  322. header[i] = avio_r8(s->pb);
  323. if (header[i] == '\n') {
  324. header[i+1] = 0;
  325. break;
  326. }
  327. }
  328. if (s->pb->error)
  329. return s->pb->error;
  330. else if (s->pb->eof_reached)
  331. return AVERROR_EOF;
  332. else if (i == MAX_FRAME_HEADER)
  333. return AVERROR_INVALIDDATA;
  334. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
  335. return AVERROR_INVALIDDATA;
  336. width = st->codec->width;
  337. height = st->codec->height;
  338. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  339. if (packet_size < 0)
  340. return packet_size;
  341. ret = av_get_packet(s->pb, pkt, packet_size);
  342. if (ret < 0)
  343. return ret;
  344. else if (ret != packet_size)
  345. return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
  346. if (s->streams[0]->codec->coded_frame) {
  347. s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  348. s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
  349. }
  350. pkt->stream_index = 0;
  351. return 0;
  352. }
  353. static int yuv4_probe(AVProbeData *pd)
  354. {
  355. /* check file header */
  356. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
  357. return AVPROBE_SCORE_MAX;
  358. else
  359. return 0;
  360. }
  361. #if CONFIG_YUV4MPEGPIPE_DEMUXER
  362. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  363. "yuv4mpegpipe",
  364. NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  365. sizeof(struct frame_attributes),
  366. yuv4_probe,
  367. yuv4_read_header,
  368. yuv4_read_packet,
  369. .extensions = "y4m"
  370. };
  371. #endif